I saw sample code on express website:
var express = require('express');
var app = express();
// simple logger
app.use(function(req, res, next){
console.log('%s %s', req.method, req.url);
next();
});
// respond
app.use(function(req, res, next){
res.send('Hello World');
});
app.listen(3000);
Here app.use() called functions of exactly the same signature and yet is able to run them in sequence. How is this done in javascript?
Here's definition for app.use(): https://github.com/senchalabs/connect/blob/master/lib/proto.js
Connect keeps a "stack" (an Array) of middleware and route handlers. And when a request is processed, it simply iterates through all handler functions in the stack in order and (subject to some route matching rules), invokes the handler functions.
this.stack.push({ route: route, handle: fn });
Related
Im setting up an express server and I want to console.log() something every time you load a page successfully.
app.get('/index.html', function(req, res, next){
res.sendFile(__dirname + "/index.html");
next();
});
app.get('/index.html', function(req, res){
console.log("test");
});
I expected it to console.log("test") every time you go to the page called index.html, but instead it doesn't log anything.
app.get registers a route, not middleware.
Since you have registered 2 routes for /index.html, the first one is found and the second one is never reached.
You need app.use to register middleware.
Also, the middleware needs to call the third argument of the callback to indicate when it is finished.
Order matters. You need to register you middleware before the route, otherwise, the route will get the request, respond to it, then stop.
const express = require('express');
const app = express();
app.use("/", (req, res, next) => {
console.log("Middleware");
next();
});
app.get("/", (req, res) => {
res.send("Hello, world");
});
app.listen(8080);
Further reading: Using middleware
I have tried almost everything, and my mind has now finally given up. Hence, I'm here asking for directions. How on earth can this simple piece of code (let's call it app.js):
var express = require('express');
var app = express();
app.use(function(req, res) {
console.log('Request made')
res.send('Hello World!');
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!');
});
When run like node app.js result in the following outcome in my terminal, considering the fact that I've only hit localhost:3000 once:
Example app listening on port 3000!
Request made
Request made
Request made
Request made
Request made
Request made
Request made
...continues...
For some reason I end up with "Request made" being repeated again and again although the request is done and delivered to the browser.
The same is true if I use app.get('*', function(req, res) { ... }) instead of app.use(function(req, res) { ... }).
If I turn to something more specific, like app.get('/', function(req, res) { ... }) the repetition stops and I get one "Request made" as expected. However, I need to match all incoming requests - in my app I'm doing something way more complicated than logging "Request made" :)
Any ideas? Thanks!
Node: 8.11.2
Express: 4.16.3
You need to do something and then pass the results to next callback instead of sending a response which makes it recursive since it's intercepting a response and then doing the same causing a loop.
// this middleware will not allow the request to go beyond it
app.use(function(req, res, next) {
res.send('Hello World');
});
// requests will never reach this route
app.get('/', function (req, res) {
res.send('Welcome');
});
You need to call next() to invoke middleware
app.get('/', function (req, res, next) {
try {
// do something and return result
res.send('Welcome');
} catch(e) {
next(e)
}
});
app.use(function (err, req, res, next) {
console.log('Error occurred')
res.status(err.statusCode).send(err.message);
});
I've been trying a variety of setups for hot reloading and one that I've come across is the https://github.com/glenjamin/ultimate-hot-reloading-example/. Modifying this boilerplate code as a starting point, I've come across the following problem in my server code:
// server.js
import chokidar from 'chokidar';
import express from 'express';
const app = express();
// this is the middleware for handline all of my routes
app.use(function (req, res, next) {
require('./server/index')(req, res, next);
// if I commented out any additional routes, the setup would work fine
require('./server/foo')(req, res, next);
require('./server/catch-all')(req, res, next);
});
//this watches the server folder for changes
const watcher = chokidar.watch('./server');
watcher.on('ready', function () {
watcher.on('all', function () {
console.log("Clearing /server/ module cache from server");
Object.keys(require.cache).forEach(function (id) {
if (/[\/\\]server[\/\\]/.test(id)) delete require.cache[id];
});
});
});
app.listen(3000, 'localhost', function (err) {
if (err) throw err;
const addr = this.address();
console.log('Listening at http://%s:%d', addr.address, addr.port);
});
The above is the server code that handles clearing the cache by watching for changes with the chokidar module. If I have just one route required inside the app.use middleware function (which listens for every incoming request), I can get it to work. However if have multiple routes, the following error occurs:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
This is a common issue posted on stack overflow, but all of the solutions I've come across and attempted haven't worked. My route files are as follows:
//index.js
import express from 'express';
const router = express.Router();
router.get('/', (req, res, next) => {
res.send("greagrehgarhegrehuh").end();
return next('router');
});
module.exports = router;
//end of index.js
//foo.js
import express from 'express';
const router = express.Router();
router.get('/foo', (req, res, next) => {
res.send("foo").end();
return next('router');
});
module.exports = router;
//end of foo.js
//catch-all.js
import express from 'express';
const router = express.Router();
router.get('*', (req, res, next) => {
res.send("catch all").end();
return next('router');
});
module.exports = router;
// end of catch-all.js
All three routes do the same thing, bar the endpoint. So far I've explicitly called end on each to end the response, used return next('router') to skip the rest of the middleware functions and have also tried doing it without the above as well. Any ideas on what I'm missing here to get this working? Here's a github project that showcases the issue
https://github.com/RonanQuigley/express-chokidar-hot-reload
UPDATE
So I actually removed the next calls and seem to have almost got it working by doing the following:
app.use(function (req, res, next) {
require('./server/index')(req, res, next);
require('./server/foo')(req, res, next);
});
// a second app.use middleware, that does the same
// as the catch all // * router.get from my original post
app.use(function (req, res, next) {
app.get('*', (req, res) => res.send('catch all'));
})
However, I can't use this second app.use with another require call to a file with an express router like the others. So it seems that express runs through the middleware stack, reaches the * and tries to set the header twice.
The reason I need the * is normally if a user requests an endpoint that doesn't exist, Node correctly shows up with cannot GET/. However, for some reason, with the setup I've outlined express will then crash. My workaround is using * at the end of the middleware stack and I'd just use a res.redirect to send the user back to wherever, but this causes the above issue I've outlined in my original post. So not sure how to get around that one.
So currently I have either:
1) Hot reloading works without the require for a router.get('*'), but when the user navigates to an endpoint that doesn't exist, express will crash.
2) Hot reloading works with the app.get('*') inside a second app.use call, but I can't then use a router to move this into a separate file.
Okay, so posting this solution up for my own future reference and in case somebody else stumbles into this problem.
After speaking with the express devs, it turns out that this is indeed possible with a combination of the following:
// you need to use comma separated routes
app.use(
dynamic('./server/index'),
dynamic('./server/foo')
);
// require the library at runtime and apply the req, res, next arguments
function dynamic(lib) {
return function (req, res, next) {
return require(lib).apply(this, arguments)
}
}
In the case of webpack, this would break it as you can't use require as an expression. So use the following to get around that:
function createRoutes(router) {
const dynamic = (lib) => {
return function (req, res, next) {
// let webpack generate a regex expression from this require
// if we don't you would get a critical dependency warning
// which would result in the routes not being found
return require("./src/" + lib + ".js").apply(this, arguments);
}
}
router.use(
dynamic('index'),
dynamic('foo'),
);
return router;
}
Let's step back a bit and talk about middleware.
Say you have a function which runs some kind of middleware.
const runMiddleware = (req, res, next) => {
console.log(`this will run everytime a HTTP request comes in`);
}
Then to use that middleware within express:
app.use(runMiddleware);
Every time any (GET, POST, DELETE, etc) request comes in, this function is run.
Essentially you are doing the same thing below - You are wrapping three (3) route calls with a single function. This function is calling all of these routes at once, hence res is actually being sent 3 times in a row in the example below:
app.use(function (req, res, next) { // runs every time any request comes in
require('./server/index')(req, res, next); // res sent, ok
require('./server/foo')(req, res, next); // res sent, err
require('./server/catch-all')(req, res, next); // res sent, err
});
Here is a basic way of handling routes:
const index = require('./server/index');
const foo = require('./server/foo');
app.use('/', index);
app.use('/foo', foo);
// catch everything else
app.use(function (req, res) {
res.send('catch all');
})
The express generator generates the following code in the app.js page:
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
However, in the documentation, it says
Since path defaults to "/", middleware mounted without a path will be executed for every request to the app.
And gives this example:
// this middleware will not allow the request to go beyond it
app.use(function(req, res, next) {
res.send('Hello World');
})
// requests will never reach this route
app.get('/', function (req, res) {
res.send('Welcome');
})
So, how could the 404 page ever be reached (or the /users route for that matter) if nothing can get passed the app.use('/', routes) line?
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
Let's say your app.js has the above code (straight from the generator) and your server receives a request to /foo. First, your app.use('/', routes); middleware gets to check if it can handle the request, because it is defined first in your middleware stack. If the routes object contains a handler for /foo, and that handler calls res.send(), then your server is done handling the request, and the rest of your middleware doesn't do anything. However, if the handler for /foo calls next() instead of res.send(), or if the routes object does not contain a handler for /foo at all, then we continue going down the list of middleware.
The app.use('/users', users); middleware does not execute, since the request is not to /users or to /users/*.
Finally, the 404 middleware executes last, since it was defined last. Since it was defined with no route, it executes for all requests that get past the first two middleware.
Is it possible to wrap every request coming through express.js in a domain or trycatch see trycatch info here?
I am trying to create a 'catch all' of sorts (the express error handler middleware does not catch async calls) to be sure any errors I miss are handled with a 500 being sent to the user.
If you have an asynchronous function call (eg. process.nextTick()), then it will be outside the scope of express' error handler, thus killing the process entirely. Thus, using the express error handler will not work in all cases.
Express already has error handler implementation. It inherit it from connect. To use it you need to add it as the last middleware point (last app.use(...) call). For example:
var express = require('express')
, app = express();
app.use(app.router);
app.use(express.errorHandler());
// app.get(...), app.post(...), app.listen(...), etc.
If you want to handle all errors with simple 500 response code, you could replace express.errorHandler() with you own function. In that case your code will looks like:
var express = require('express')
, app = express();
app.use(app.router);
app.use(function(err, req, res, next){
if (!err) return next();
res.send(500);
});
// app.get(...), app.post(...), app.listen(...), etc.
More information about that way could be found in express error example comments in code
UPDATE:
Of course you could use domain for each request. You could wrap each request separately or use wrapping for router to handle ALL exceptions. Code is following:
var express = require('express')
, http = require('http')
, app = express()
, domain = require('domain');
//app.use(app.router);
app.use(function(req, res, next){
var d = domain.create();
d.on('error', function(er) {
console.log('error, but oh well', er.message);
res.send(500);
});
// explicitly add req and res
d.add(req);
d.add(res);
d.run(function() {
app.router(req, res, next);
});
});
app.get('/', function(req,res){
process.nextTick(function(){
throw new Error('Check Error');
});
});
http.createServer(app).listen(3000, function(){
console.log('Express server listening on port 3000');
});
!!BUT!! never use this in production. The reason of that is in nature how JS throw work. It will definitely be a cause of leaking in your application and make it even more unstable. You could use such error handling to implement custom algorithm of shutdown (for example to close already opened connection). More information about right use of domain could be found in documentation.
To monitor the leaking you could use the technique from this article.
UPDATE 2:
I just can't leave this not finished. trycatch code:
var express = require('express')
, http = require('http')
, app = express()
, domain = require('domain')
, trycatch = require('trycatch');
//app.use(app.router);
app.use(function(req, res, next){
trycatch(function(){
app.router(req, res, next);
}, function(er){
console.log(er.message);
res.send(500);
});
});
app.get('/', function(req,res){
process.nextTick(function(){
throw new Error('Check Error');
});
});
http.createServer(app).listen(3000, function(){
console.log('Express server listening on port 3000');
});
I had review the source of trycatch and there was no any magic. It still be cause of leaks. trycatch has domain under the hood.