Node.js – get raw request body using Express [duplicate]

Edit 2: Release 1.15.2 of the body parser module introduces raw mode, which returns the body as a Buffer. By default, it also automatically handles deflate and gzip decompression. Example usage: var bodyParser = require(‘body-parser’); app.use(bodyParser.raw(options)); app.get(path, function(req, res) { // req.body is a Buffer object }); By default, the options object has the following … Read more

Global Variable in app.js accessible in routes?

It is actually very easy to do this using the “set” and “get” methods available on an express object. Example as follows, say you have a variable called config with your configuration related stuff that you want to be available in other places: In app.js: var config = require(‘./config’); app.configure(function() { … app.set(‘config’, config); … … Read more

How to protect the password field in Mongoose/MongoDB so it won’t return in a query when I populate collections?

You can change the default behavior at the schema definition level using the select attribute of the field: password: { type: String, select: false } Then you can pull it in as needed in find and populate calls via field selection as ‘+password’. For example: Users.findOne({_id: id}).select(‘+password’).exec(…);

Difference Between app.use() and router.use() in Express

router.get is only for defining subpaths. Consider this example: var router = express.Router(); app.use(‘/first’, router); // Mount the router as middleware at path /first router.get(‘/sud’, smaller); router.get(‘/user’, bigger); If you open /first/sud, then the smaller function will get called. If you open /first/user, then the bigger function will get called. In short, app.use(‘/first’, router) mounts … Read more

Catch all route EXCEPT for /login

I’m not sure what you want to happen when a user accesses /login or /, but you can create separate routes for those; if you declare them before the catch-all, they get first dibs at handling the incoming requests: app.get(‘/login’, function(req, res) { … }); app.get(“https://stackoverflow.com/”, function(req, res) { … }); app.get(‘*’, function(req, res) { … Read more

Function to convert timestamp to human date in javascript [duplicate]

The value 1382086394000 is probably a time value, which is the number of milliseconds since 1970-01-01T00:00:00Z. You can use it to create an ECMAScript Date object using the Date constructor: var d = new Date(1382086394000); How you convert that into something readable is up to you. Simply sending it to output should call the internal … Read more

How does one unit test routes with Express?

As others have recommended in comments, it looks like the canonical way to test Express controllers is through supertest. An example test might look like this: describe(‘GET /users’, function(){ it(‘respond with json’, function(done){ request(app) .get(“https://stackoverflow.com/users”) .set(‘Accept’, ‘application/json’) .expect(200) .end(function(err, res){ if (err) return done(err); done() }); }) }); Upside: you can test your entire stack … Read more

Node.js/Express.js App Only Works on Port 3000

The following works if you have something like this in your app.js: http.createServer(app).listen(app.get(‘port’), function(){ console.log(“Express server listening on port ” + app.get(‘port’)); }); Either explicitly hardcode your code to use the port you want, like: app.set(‘port’, process.env.PORT || 3000); This code means set your port to the environment variable PORT or if that is undefined … Read more