What I am trying to achieve is my statics being loaded on / , with the api mounted at /api
Here is my main file:
var express = require('express');
var server = express();
var app = require('./api/app');
server.use(express.static('/', __dirname + '/public'));
server.use('/api', app(server));
server.listen(3000);
My app.js file:
module.exports = function(app) {
app.get('/users/:id', function(req, res, next){
res.json(req.params);
});
}
I am getting a Cannot read property 'handle' of undefined error. I still haven't quite got my head around express' use and i'm sure im making a very novice mistake but just not sure how I can configure to get the result I would like.
Thanks.
You should use a Router instead with Express 4.x:
// main.js
var express = require('express');
var server = express();
var api = require('./api/app');
server.use(express.static('/', __dirname + '/public'));
server.use('/api', api());
server.listen(3000);
// ./api/app.js
var router = require('express').Router();
module.exports = function() {
router.get('/users/:id', function(req, res, next){
res.json(req.params);
});
return router;
};
use expects to receive a middleware function, but in this example it's not actually getting anything (notice that there's no return value in app.js). One way to fix this would be to return an instance of express' Router middleware:
var express = require('express');
module.exports = function(app) {
var apiRouter = express.Router();
apiRouter.get('/users/:id', function(req, res, next){
res.json(req.params);
});
return apiRouter;
};
Related
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;
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;
basically im just trying to seprate routes, models, and controller in node.js application.
i have following files to setup very very basic node.js application.
controller/cv.js
module.exports = {
get: function(req, res, next){
console.log("GET REQUESTS")
next();
}
}
routes/cv.js
var express = require('express');
var CvRouter = express.Router();
var CvController = require('../controller/cv')
CvRouter.get('/', function(req, res, next){
console.log("GET REQUESTS")
next();
})
module.export = CvRouter
app.js
const express = require('express');
const bodyParser= require('body-parser')
var path = require('path')
const app = express();
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
var router = express.Router();
require('./router')(app)
app.listen(3000, function() {
console.log('listening on 3000')
})
router.js
var CvRouter = require('./routes/cv')
module.exports = function(app) {
app.use([CvRouter]);
};
Basicaly this last file router.js is generting error when i use app.use([CvRouter])
ERROR is: throw new TypeError('app.use() requires middleware functions');
how i can resolve it? i also know its returning object of router. and app.use expecting function in parameter. but how i can achieve my desired MVC pattern of node.js?
as said in comment - you have a typo.
The file routes/cv.js contains module.export instead of module.exports, that makes CvRouter undefined.
Kill the array literal
var CvRouter = require('./routes/cv')
module.exports = function(app) {
app.use(CvRouter);
};
Hi I've seen similar errors, but none of them apply to this circumstance. I'm working through the Getting Mean book but applying all the lessons on Express 3 to an Express 4 app.
In app.js
app.set('views', path.join(__dirname, 'app_server/views'));
//index.jade is located here and confirmed to work before moving controller
...
require('./routes')(app);
//Instead of app.use('/', routes);
In routes/index.js
var ctrl = require('../app_server/controllers/main');
module.exports = function (app) {
app.get('/', ctrl.index);
};
In app_server/controllers/main.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('index', { title: 'Loc8r' });
});
// /* Also tried */
// exports.index = function(req, res){
// res.render('index', { title: 'Express' });
// };
Running the app yields "Error: Route.get() requires callback functions but got a [object Undefined]" on the res.render('index', { title: 'Loc8r' }); line.
Been at this for hours. Help is much appreciated.
The error is likely caused by this line in route/index.js:
app.get('/', ctrl.index);
While app_server/controllers/main.js now makes use of express.Router, route/index.js hasn't been updated to match. It's still expecting a ctrl.index, which is no longer being defined.
console.log(ctrl); // {}
console.log(ctrl.index); // undefined
app_server/controllers/main.js should export the Router it's defining:
var express = require('express');
var router = module.exports = express.Router();
// ^^^^^^^^^^^^^^
// ...
So it can be used as middleware in routes/index.js:
module.exports = function (app) {
app.use(require('../app_server/controllers/main'));
};
[Edit] Regarding my comment below:
/** app_server/controllers/main.js **/
var express = require('express');
var router = module.exports = express.Router();
// ...
/** routes/index.js **/
var express = require('express');
var router = module.exports = express.Router();
router.use(require('../app_server/controllers/main.js'));
// ...
// router.use('/area1', require('../app_server/controllers/area1.js'));
// router.use('/area2', require('../app_server/controllers/area2.js'));
/** app.js **/
// ...
app.use(require('./routes'));