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?
This route router.get('/inscription', ...) in your inscription router is configured for the route /inscription/inscription which is likely not what you intended. This is because you've specified it in two places:
app.use('/inscription', inscription);
router.get('/inscription', ...)
So, the whole router is on /inscription from the app.use('/inscription', inscription). That means that any route the router itself defines will be added to that path.
It isn't exactly clear from your question exactly what you intend for the URLs to be. But, if you just want the above router.get() to work for a /inscription URL, then change:
router.get('/inscription', ...)
to:
router.get('/', ...)
When you use app.use('/inscription', inscription);, every single route in that router will be prefixed with /inscription. So, this route:
router.post('/adduser', ...)
will be mounted at:
/inscription/adduser
Or, if you want all the inscription routes to be at the top level too, then change:
app.use('/inscription', inscription);
to this:
app.use('/', inscription);
So that nothing is added to the path beyond what the router itself defines.
Related
I can't work out where I'm going wrong with this simple routing task. When I go to localhost:3030/staff, I'm getting "Cannot GET /staff" and a 404 error.
Here is my setup.
app.js:
const express = require('express');
const app = express();
const port = process.env.PORT || 3030;
const staffRouter = require('./routes/staffrouter.js');
app.use('/static', express.static(__dirname + './public'));
app.use('./staff', staffRouter);
app.get('/', function(req, res) {
res.render('index.pug');
});
app.listen(port);
I have tried using the paths "/staff" in my GET/POST requests, but that doesn't work, and isn't how it's supposed to work according to the tutorial I'm doing. I'm really stuck.
/routes/staffrouter.js:
var express = require('express');
var router = express.Router();
const staff = require('../staff').staff;
const urlEncoded = (express.urlencoded({ extended: true }));
router.get('/', function(req, res, next) {
res.render('staff.pug', {
deptOptions: staff.populateSelectors('department'),
posOptions: staff.populateSelectors('position'),
empArray: staff.readWriteJSON()
});
});
// Add new staff obj
router.post('/', urlEncoded, function(req, res, next) {
let sObj = req.body;
let dataArray = staff.readWriteJSON();
//console.log('data:', data);
dataArray.push(new staff.Employee(
sObj.fName,
sObj.lName,
sObj.staffNum,
sObj.department,
sObj.position,
sObj.email,
sObj.phone
));
staff.readWriteJSON(dataArray)
res.render('../views/staff.pug', {
deptOptions: staff.populateSelectors('department'),
posOptions: staff.populateSelectors('position'),
empArray: staff.readWriteJSON()
});
});
module.exports = router;
wrong scope change the order
app.use('/static', express.static(__dirname + './public'));
const staffRouter = require('./routes/staffrouter.js');
I have a route file in my project and it is called from my app with these lines:
var index = require('./routes/index');
app.use('/', index);
But I need to separate the route file, and I'm trying to do this:
var index = require('./routes/index');
var user = require('./routes/user');
app.use('/', index);
app.use('/user', user);
In route user.js I put the service that I need to access from the client. But it's not working. I don't know what is wrong, I am a beginner in Node.js.
The request returns:
GET /user/find 304 4.203 ms - -
And user.js file is:
var router = express.Router();
router.get('/user/find',function(req, res){
Object.find(function(err, s){
if(err) res.send(err);
res.json(s);
});
});
module.exports = router;
*This request works well on index.js
You put user router under /user route, and in your user router you defined app.get('/user/find'), so the actual path would be /user/user/find, you need to remove the user prefix in router
var router = express.Router();
router.get('/find',function(req, res){
Object.find(function(err, s){
if(err) res.send(err);
res.json(s);
});
});
module.exports = router;
A simple way to do this can be:
index.js
var express = require('express')
var app = express()
var route1 = require('./route1')
var route2 = require('./route2')
app.use('/', route1);
app.use('/hello', route2);
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
route1.js
var express = require('express')
var router = express.Router()
router.get('/', function (req, res) {
res.send('Hello route1');
})
module.exports = router
route2.js
var express = require('express')
var router = express.Router()
router.get('/', function (req, res) {
res.send('Hello route2');
})
module.exports = router
Have you made sure to include a module.exports = router at the end of each of your route files?
Your route files should be set up thusly:
var router = require('express').Router();
router.get("/example", function (req, res) {
res.send("Hello");
});
module.exports = router;
I've applied the routes to my application like this:
var express = require('express');
var app = express();
var router = express.Router();
//localhost:8080/api/story
router.get('/story', function(req, res){
res.send('welcome to our story');
})
//localhost:8080/api
app.use('/api', router);
//localhost:8080/user/02213
router.get('/user/:id', function(req , res){
console.log(req.params.id);
});
localhost:8080/user/02213 not working at the moment. Do I need to create a new router instead or?
Yes, you need to create a new router, because router will only be used for requests that start with /api:
//localhost:8080/api/story
router.get('/story', function(req, res){
res.send('welcome to our story');
})
//localhost:8080/api
app.use('/api', router);
//localhost:8080/user/02213
var anotherRouter = express.Router();
anotherRouter.get('/user/:id', function(req , res){
console.log(req.params.id);
res.end();
});
app.use('/', anotherRouter);
The problem is that id not recognized in the request,
You should call it like that : req.params.id
This is my app.js
var express = require('express');
...
var model = require('./models/my_model');
var routes = require('./routes/index');
var app = express();
app.use('/', routes);
var middlewareBefore = function(req, res, next){
my_model.getName(function(err, result){
if(err){
console.log(err);
res.redirect('/error');
}else{
console.log(result);
next();
}
})
}
app.use(middlewareBefore);
...
module.exports = app;
and this is my routes/index.js file
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('login', { url: my_model.getUrl() });
});
module.exports = router();
I'm trying to access my_model from the route file. I've already tried using app.locals, res, req, res.locals and app.use('/', routes)(my_model), as seen on different questions from this page, but none of them seem to work... I wonder if the usage of express.Router() is creating this issue (note that setting the middleware before the route didn't solve this)
Several ways to do that:
1.use app.use('/', routes); after declaration of middlewareBefore variable.
2.or declare my_model directly in routes/index.js at top var model = require('./models/my_model');
and use within router.get().
3.or better use MVC architecture.
I write my app.js including all the routes in the main file and everything was working well. After my goal was to make the project more clear by moving the routes in a different files but it is not working.
I'm passing an object instead of a middleware function and I don't know how to fix it in the right way.
So this is my app.js file:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var myRoutes = require('./app/routes/myRoutes.js');
...
//parser for getting info from POST and/or URL parameters
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//for log requests to console
app.use(morgan('dev'));
app.use('/myRoutes', myRoutes);
app.get('/',function(req,res){
res.end('Welcome Page!');
});
//Server Start
app.listen(port);
console.log('server start at port ' + port);
And the app/routes/myRoutes.js contains the following code:
var express = require('express');
...
var myRoutes = express.Router();
myRoutes.get('/users',function(req,res){
...
});
myRoutes.post('/setup',function(req,res){
...
});
myRoutes.post('/remove', function(req,res){
...
});
module.export = myRoutes;
I also tried this:
var express = require('express');
var myRoutes = express.Router();
myRoutes.route('/')
.get(function(req, res, next){
res.end('myRoute Get');
})
.post(function(req, res, next){
res.end('myRoute Post');
});
module.export = myRoutes;
But again it seems not passing a middleware function.
My second option code
var express = require('express');
var myRoutes = express.Router();
myRoutes.route('/')
.get(function(req, res, next){
res.end('myRoute Get');
})
.post(function(req, res, next){
res.end('myRoute Post');
});
module.export = myRoutes;
is working fine! I just write it in a wrong way
module.export = myRoutes;
isntead of
module.exports = myRoutes;
Hi this is more of additional tips on the question. You main js file would definately need to load a lot of routes and i found importing all of them is a lot of work. Rather use require-dir module to load all the routes like
const loader = require('require-dir');
var app = express();
var routes = loader('./routes');
for (route in routes){
app.use("/"+route,routes[route]);
}
needless to say define all routes inside routes folder and export Router module in each one of them like
var router = express.Router();
router.get(....);
module.exports = router;