How To Create Mongoose Schema with Array of Object IDs?

If you want to use Mongoose populate feature, you should do: var userSchema = mongoose.Schema({ email: { type: String, required: true, unique: true}, password: { type: String, required: true}, name: { first: { type: String, required: true, trim: true}, last: { type: String, required: true, trim: true} }, phone: Number, lists: [listSchema], friends: [{ type … Read more

ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client

That particular error occurs whenever you try to send more than one response to the same request and is usually caused by improper asynchronous code. The code you show in your question does not appear like it would cause that error, but I do see code in a different route here that would cause that … Read more

How do sessions work in Express.js with Node.js?

Overview Express.js uses a cookie to store a session id (with an encryption signature) in the user’s browser and then, on subsequent requests, uses the value of that cookie to retrieve session information stored on the server. This server side storage can be a memory store (default) or any other store which implements the required … Read more

How to enable cross-origin resource sharing (CORS) in the express.js framework on node.js

Check out the example from enable-cors.org: In your ExpressJS app on node.js, do the following with your routes: app.all(“https://stackoverflow.com/”, function(req, res, next) { res.header(“Access-Control-Allow-Origin”, “*”); res.header(“Access-Control-Allow-Headers”, “X-Requested-With”); next(); }); app.get(“https://stackoverflow.com/”, function(req, res, next) { // Handle the get for this route }); app.post(“https://stackoverflow.com/”, function(req, res, next) { // Handle the post for this route }); … Read more

Using HTML in Express instead of Jade

You can do it this way: Install ejs: npm install ejs Set your template engine in app.js as ejs // app.js app.engine(‘html’, require(‘ejs’).renderFile); app.set(‘view engine’, ‘html’); Now in your route file you can assign template variables // ./routes/index.js exports.index = function(req, res){ res.render(‘index’, { title: ‘ejs’ });}; Then you can create your html view in … Read more

Using PassportJS, how does one pass additional form fields to the local authentication strategy?

There’s a passReqToCallback option that you can enable, like so: passport.use(new LocalStrategy( {usernameField: ’email’, passReqToCallback: true}, function(req, email, password, done) { // now you can check req.body.foo } )); When, set req becomes the first argument to the verify callback, and you can inspect it as you wish.

Use specific middleware in Express for all paths except a specific one

I would add checkUser middleware to all my paths, except homepage. app.get(“https://stackoverflow.com/”, routes.index); app.get(‘/account’, checkUser, routes.account); or app.all(‘*’, checkUser); function checkUser(req, res, next) { if ( req.path == “https://stackoverflow.com/”) return next(); //authenticate user next(); } You could extend this to search for the req.path in an array of non-authenticated paths: function checkUser(req, res, next) { … Read more

Passing route control with optional parameter after root in express?

That would work depending on what client.get does when passed undefined as its first parameter. Something like this would be safer: app.get(‘/:key?’, function(req, res, next) { var key = req.params.key; if (!key) { next(); return; } client.get(key, function(err, reply) { if(client.get(reply)) { res.redirect(reply); } else { res.render(‘index’, { link: null }); } }); }); There’s … Read more