Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add templates #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var axios = require('axios');
var PORT = process.env.PORT || 3000;
const BASE_URL = `http://localhost:${PORT}`;


async function getUsers(){
let users;
try {
users = await axios.get(`${BASE_URL}/users`);
} catch(error) {
throw (error);
}
return users.data;
};


module.exports = {
getUsers
}
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
var debug = require('debug')('frontend-code-challenge');
var express = require('express');
var nunjucks = require( 'nunjucks' ) ;
var path = require('path');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('./lib/logger');
var cors = require('cors');
var apiHelper = require('./api');

var users = require('./routes/users');

var app = express();
var log = logger(app);

nunjucks.configure( 'views', {
autoescape: true,
express: app
} ) ;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
// app.use(express.static(path.join(__dirname, 'public')));

app.use('/users', users);

app.get('/', function(req, res, next){
return res.render('index.html');
});

app.get('/signup', function(req, res, next){
return res.render('signup.html');
});

app.get('/admin', async function(req, res, next){

const response = await apiHelper.getUsers();

return res.render('admin.html', {
users: response
});
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
Expand Down
164 changes: 164 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
"start": "node index.js"
},
"dependencies": {
"axios": "^0.21.0",
"body-parser": "~1.8.1",
"cookie-parser": "~1.3.3",
"cors": "^2.8.4",
"debug": "~2.0.0",
"express": "~4.9.0",
"express-winston": "^0.2.9",
"lodash": "^2.4.1",
"nunjucks": "^3.2.2",
"serve-favicon": "~2.1.3",
"winston": "^0.8.3"
}
Expand Down
9 changes: 8 additions & 1 deletion routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ router.get('/', function(req, res) {

/* Create a new user */
router.post('/', function(req, res) {

var user = req.body;
user.id = curId++;
if (!user.state) {
user.state = 'pending';
} else {
user.state = 'active';
}
users[user.id] = user;
log.info('Created user', user);
res.json(user);
// res.json(user);

return res.render('index.html', {
message: `Hi, ${user.firstName}. Thank you for signing up`
});
});

/* Get a specific user by id */
Expand Down
Loading