Answer the question
In order to leave comments, you need to log in
How to put routes into separate files?
There is a file that specifies the creation of a server on express and a bunch of routes by type
app.get('/user', function(req, res) {
if(req.isAuthenticated() && req.user.role == 'user')
res.render('user');
});
app.post('/changeRole', function(req, res){
console.log(req.body);
pg.connect(connectionString, function(err, client, done){
console.log(req);
if(err){
return console.error('error feetching client from pool', err);
}
client.query('UPDATE items SET role=($1), change=($2) WHERE name=($3)', [req.body.role,
req.body.role == 'user' ? 'true' : 'false', req.body.name]);
done();
});
});
Answer the question
In order to leave comments, you need to log in
Let's say we name the file handlers.js with the following content
module.exports.getUser = (req, res) => {
if (req.isAuthenticated() && req.user.role == 'user')
res.render('user');
}
module.exports.postChangeRole = (req, res) => {
console.log(req.body);
pg.connect(connectionString, function (err, client, done) {
console.log(req);
if (err) {
return console.error('error feetching client from pool', err);
}
client.query('UPDATE items SET role=($1), change=($2) WHERE name=($3)', [req.body.role,
req.body.role == 'user' ? 'true' : 'false', req.body.name]);
done();
});
}
var handlers=require('./handlers');
app.get('/user', handlers.getUser);
app.post('/changeRole',handlers.postChangeRole);
Take this thing
function(req, res) {
if(req.isAuthenticated() && req.user.role == 'user')
res.render('user');
module.expotrts = function(req, res) {
if(req.isAuthenticated() && req.user.role == 'user')
res.render('user');
};
foo.js
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question