I have some trouble using the router from Express. I want to set up my routes with several files. I get my routes folder with 2 files: routes.js and inscription.js
I do the following
var inscription = require('./routes/inscription.js');
var routes = require('./routes/routes.js');
Then
app.use('/', routes);
app.use('/inscription', inscription);
But only the routes from routes.js work...
This is the content of routes.js
var router = require('express').Router();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false});
//Homepage
router.get('/', function(req, res){
res.setHeader('Content-Type', 'text/html');
res.status(200);
res.render('home.ejs');
});
//Connexion
router.post('/connexion', urlencodedParser, function(req, res){
//Some content
});
module.exports = router;
And this is the content of inscription.js
var router = require('express').Router();
var hash = require('password-hash');
var db = require('../models/users.js');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false});
router.get('/inscription', function(req, res){
res.setHeader('Content-Type', 'text/html');
res.status(200);
res.render('inscription.ejs');
});
router.post('/adduser', urlencodedParser, function(req, res){
var passwordHashed = hash.generate(req.body.inputPassword);
var newUser = {
nom : req.body.inputName,
email : req.body.inputEmail,
password : passwordHashed
};
db.addUser(newUser);
res.redirect('/');
});
router.post('/checkname', urlencodedParser, function(req, res){
var user = {
nom : req.body.inputName
};
db.checkName(user, function(length){
res.send(length);
});
});
router.post('/checkemail', urlencodedParser, function(req, res){
var user = {
email : req.body.inputEmail
};
db.checkEmail(user, function(length){
res.send(length);
});
});
module.exports = router;
The content of inscription.js works when it is pasted in the routes.js file ... So I guess it is how I import the file that is not working.
Any idea?