Avoid “current URL string parser is deprecated” warning by setting useNewUrlParser to true

Check your mongo version: mongo –version If you are using version >= 3.1.0, change your mongo connection file to -> MongoClient.connect(“mongodb://localhost:27017/YourDB”, { useNewUrlParser: true }) or your mongoose connection file to -> mongoose.connect(“mongodb://localhost:27017/YourDB”, { useNewUrlParser: true }); Ideally, it’s a version 4 feature, but v3.1.0 and above are supporting it too. Check out MongoDB GitHub … Read more

File uploading with Express 4.0: req.files undefined

The body-parser module only handles JSON and urlencoded form submissions, not multipart (which would be the case if you’re uploading files). For multipart, you’d need to use something like connect-busboy or multer or connect-multiparty (multiparty/formidable is what was originally used in the express bodyParser middleware). Also FWIW, I’m working on an even higher level layer … Read more

How to access the request body when POSTing using Node.js and Express?

Starting from express v4.16 there is no need to require any additional modules, just use the built-in JSON middleware: app.use(express.json()) Like this: const express = require(‘express’) app.use(express.json()) // <==== parse request body as JSON app.listen(8080) app.post(‘/test’, (req, res) => { res.json({requestBody: req.body}) // <==== req.body will be a parsed JSON object }) Note – body-parser, … Read more

How to include route handlers in multiple files in Express? [duplicate]

If you want to put the routes in a separate file, for example routes.js, you can create the routes.js file in this way: module.exports = function(app){ app.get(‘/login’, function(req, res){ res.render(‘login’, { title: ‘Express Login’ }); }); //other routes.. } And then you can require it from app.js passing the app object in this way: require(‘./routes’)(app); … Read more

Extend Express Request object using Typescript

You want to create a custom definition, and use a feature in Typescript called Declaration Merging. This is commonly used, e.g. in method-override. Create a file custom.d.ts and make sure to include it in your tsconfig.json‘s files-section if any. The contents can look as follows: declare namespace Express { export interface Request { tenant?: string … Read more

Passing variables to the next middleware using next() in Express.js

This is what the res.locals object is for. Setting variables directly on the request object is not supported or documented. res.locals is guaranteed to hold state over the life of a request. res.locals (Note: the documentation quoted here is now outdated, check the link for the most recent version.) An object that contains response local … Read more

Push items into mongo array via mongoose

Assuming, var friend = { firstName: ‘Harry’, lastName: ‘Potter’ }; There are two options you have: Update the model in-memory, and save (plain javascript array.push): person.friends.push(friend); person.save(done); or PersonModel.update( { _id: person._id }, { $push: { friends: friend } }, done ); I always try and go for the first option when possible, because it’ll … Read more

How to search in array of object in mongodb

The right way is: db.users.find({awards: {$elemMatch: {award:’National Medal’, year:1975}}}) $elemMatch allows you to match more than one component within the same array element. Without $elemMatch mongo will look for users with National Medal in some year and some award in the year 1975, but not for users with National Medal in 1975. See MongoDB $elemMatch … Read more

How to generate unique ID with node.js

Install NPM uuid package (sources: https://github.com/kelektiv/node-uuid): npm install uuid and use it in your code: var uuid = require(‘uuid’); Then create some ids … // Generate a v1 (time-based) id uuid.v1(); // -> ‘6c84fb90-12c4-11e1-840d-7b25c5ee775a’ // Generate a v4 (random) id uuid.v4(); // -> ‘110ec58a-a0f2-4ac4-8393-c866d813b8d1’ ** UPDATE 3.1.0 The above usage is deprecated, so use this … Read more