Node.js - external middleware causing 404 - node.js

I have a simple express app (version 4.9.0) and I am trying to put my middleware in to external files.
My app.js has:
var middleware = require('./lib/middleware');
app.get('/foo', middleware.configcache);
/lib/middleware contains index.js:
exports.configcache = require('./configcache.js');
/lib/middleware/configcache.js contains:
function configcache(req, res, next) {
console.log("hello world");
next();
}
module.exports = configcache;
When I make a GET request to /foo I get a 404. Can anyone advise?

This is how I use it in one of my apps:
app.js:
var routes = require('./routes/index');
routes/index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', {
title: req.i18n.t("meta.title.index")
});
});
module.exports = router;
I scaffolded this express project with the yeoman express generator: yo express
So the answer to your problem is: Don't call next(), but send an actual response to the browser - when you are using router.get():
function configcache(req, res, next) {
res.send(200, 'hello world');
}
or use router.use like described here: http://expressjs.com/guide/using-middleware.html

Related

What does a wildcard route do in express.js?

So, I have these routes in my app.js file.
app.use("/api/v1/users", userRouter)
app.use("/*", indexRouter)
And in index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/*', function(req, res, next) {
res.render('index', { title: 'Gratitude Journal App' });
});
module.exports = router;
What does the * do exactly?
It will render index page for all of your app's GET request. It (*) will match any string(route) after it.

ExpressJS app.locals.title gives: Can't set headers after they are sent

I am new to ExpressJS, so I wanted to set a simple string value, I could translate.
So I tried out globalize-express and set the app title like this:
app.use(function (req, res, next) {
console.log("App: " + req.Globalize.formatMessage('strings/title'));
res.locals.title = req.Globalize.formatMessage('strings/title');
next();
});
followed by:
app.use('/', index);
It looks like it is rendering correctly, but the console is posting the error:
Can't set headers after they are sent.
How can I avoid this error?
I found that the error was in the related routes/index.js, there was scaffolded another next() call. I updated that file to:
var express = require('express');
var app = require("../app.js");
var router = express.Router();
router.get('/', function (req, res) {
res.render('index', {
});
});
module.exports = router;

How to add correct way to my static in Express app?

Important from my app.js file:
var express = require('express');
var profile = require('./controllers/profile-controller'); //require my controller (profile-controller.js)
app.use(express.static(path.join(__dirname, 'public')));
app.use('/user', profile);
When I go to 'mysite.com/user' - it works fine, but when I go to 'mysite.com/user/user1', my CSS and images ain't loading. Here is my controller file:
var express = require('express');
var router = express.Router();
// route for www.mysite.com/user
router.get('/', function(req, res, next) {
res.render('profile',{ title: 'Profile' });
});
// route for www.mysite.com/user/user1
router.get('/:id([A-Za-z0-9_]{5})', function(req, res, next) {
var id = req.params.id;
res.render('profile',{ title: 'Profile' });
});
module.exports = router;
Console gives me that:
So why I have the same route for both pages, but one of them working well, but second gives 404 for static files?
In your index.hbs file, you have src="images/wow.png", which is a relative path to your image resource. So when you go to /user/user1, it just adds the /user to your image resource path.
Instead of src="images/wow.png" try putting src="/public/images/wow.png" or src="/images/wow.png".

Express routing on root "/" doesn't work in app.get

I've been having this issue where for some reason the Express route doesn't see my root get function. I've declared my app.js this way:
var index = require('./app/routes/index');
var app = express();
app.use('/', index);
Then in my index.js I have my definition this way:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
console.log('Enter root.');
});
router.get('/something', function(req, res, next) {
console.log('Enter something.');
});
Express routes into '/something' just fine, but couldn't see '/'. Anybody have an idea why it doesn't work? Thanks.
Modified based on new info:
If you're getting a 304 status back in the browser, that's because the browser has cached the GET request and the server is telling the browser that the page has not been changed so the browser can just use the cached copy.
You can make the page uncacheable by changing the headers the server sends with the request.
See Cache Control for Dynamic Data Express.JS and NodeJS/express: Cache and 304 status code and Nodejs Express framework caching for more info.
You show no exports in index.js so this line:
var index = require('./app/routes/index');
does not accomplish anything. index is an empty object and thus this:
app.use('/', index);
doesn't do anything and, in fact, may even cause an error.
Perhaps what you want is this:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
console.log('Enter root.');
});
router.get('/something', function(req, res, next) {
console.log('Enter something.');
});
// export your router
module.exports = router;
Then, index in your other file will be the router.

How to use multiple router files

How do I use multiple router files using express framework?
In my app.js, I have the following code:
var controller = require('./controller/index');
var healthController = require('./controller/health/');
app.use('/', controller);
app.use('/health', healthController);
And controller/index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index');
});
module.exports = router;
And health.js:
var express = require('express');
var router = express.Router();
/* GET health confirmation. */
router.get('/health', function(req, res, next) {
res.send('OK');
});
module.exports = router;
When I hit the http://localhost:8000/, I get the correct page without any problem, however, http://localhost:8000/health results in 404 error.
Thanks in advance.
Assuming the "health.js" resides in "controller" directory, may it be just a typo issue? var healthController = require('./controller/health/'); has a trailing slash (/). Removing it would fly? So it becomes var healthController = require('./controller/health');
Your single node app must have single router object, a router object represents a server in express requiring unique port.
Hence you should create router object in you app.js passing it to all router files.
Code will be like -
app.js
var express = require('express');
var router = express.Router();
var controller = require('./controller/index');
var healthController = require('./controller/health/');
controller(router);
healthController(router);
index.js
module.exports = function(router) {
router.get('/', function(req, res, next) {
res.render('index');
});
}
health.js
module.exports = funtion(router) {
router.get('/health', function(req, res, next) {
res.send('OK');
});
}
See How to include route handlers in multiple files in Express?.
Export an anonymous function that can be "initiated" with a reference to the original express app.
./controller/index.js:
module.exports = function(app) {
/* GET home page. */
app.get('/', function(req, res, next) {
res.render('index');
});
};
./controller/health.js:
module.exports = function(app) {
/* GET health confirmation. */
app.get('/health', function(req, res, next) {
res.send('OK');
});
};
./app.js:
var app = require('express')();
var controller = require('./controller/index');
var healthController = require('./controller/health');
controller(app);
healthController(app);
Change in health.js:
router.get('/health', function(req, res, next) {
res.send('`OK`');
});
to
router.get('/', function(req, res, next) {
res.send('OK');
});
This will work fine check it out.

Resources