How can I set response header on express.js assets

There is at least one middleware on npm for handling CORS in Express: cors. [see @mscdex answer] This is how to set custom response headers, from the ExpressJS DOC res.set(field, [value]) Set header field to value res.set(‘Content-Type’, ‘text/plain’); or pass an object to set multiple fields at once. res.set({ ‘Content-Type’: ‘text/plain’, ‘Content-Length’: ‘123’, ‘ETag’: ‘12345’ … Read more

How to get all registered routes in Express?

express 3.x Okay, found it myself … it’s just app.routes 🙂 express 4.x Applications – built with express() app._router.stack Routers – built with express.Router() router.stack Note: The stack includes the middleware functions too, it should be filtered to get the “routes” only.

What is the difference between res.end() and res.send()?

I would like to make a little bit more emphasis on some key differences between res.end() & res.send() with respect to response headers and why they are important. 1. res.send() will check the structure of your output and set header information accordingly. app.get(“https://stackoverflow.com/”,(req,res)=>{ res.send(‘<b>hello</b>’); }); app.get(“https://stackoverflow.com/”,(req,res)=>{ res.send({msg:’hello’}); }); Where with res.end() you can only respond … Read more

Make Axios send cookies in its requests automatically

You can use withCredentials property. XMLHttpRequest from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before making the request. axios.get(BASE_URL + ‘/todos’, { withCredentials: true }); Also its possible to force credentials to every Axios requests axios.defaults.withCredentials = true Or using credentials for some of the … Read more

Difference between res.send and res.json in Express.js

The methods are identical when an object or array is passed, but res.json() will also convert non-objects, such as null and undefined, which are not valid JSON. The method also uses the json replacer and json spaces application settings, so you can format JSON with more options. Those options are set like so: app.set(‘json spaces’, … Read more

How to redirect 404 errors to a page in ExpressJS?

I found this example quite helpful: https://github.com/visionmedia/express/blob/master/examples/error-pages/index.js So, it is actually this part: // “app.router” positions our routes // above the middleware defined below, // this means that Express will attempt // to match & call routes _before_ continuing // on, at which point we assume it’s a 404 because // no route has handled … Read more

static files with express.js

If you have this setup /app /public/index.html /media Then this should get what you wanted var express = require(‘express’); //var server = express.createServer(); // express.createServer() is deprecated. var server = express(); // better instead server.configure(function(){ server.use(‘/media’, express.static(__dirname + ‘/media’)); server.use(express.static(__dirname + ‘/public’)); }); server.listen(3000); The trick is leaving this line as last fallback server.use(express.static(__dirname + … Read more

What is NODE_ENV and how to use it in Express?

NODE_ENV is an environment variable made popular by the express web server framework. When a node application is run, it can check the value of the environment variable and do different things based on the value. NODE_ENV specifically is used (by convention) to state whether a particular environment is a production or a development environment. … Read more