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): or I always try and go for the first option when possible, because it’ll respect more of the benefits that mongoose gives you (hooks, validation, etc.). However, if you are doing … Read more

ExpressJS : res.redirect() not working as expected?

The problem might not lie with the backend, but with the frontend. If you are using AJAX to send the POST request, it is specifically designed to not change your url. Use window.location.href after AJAX’s request has completed (in the .done()) to update the URL with the desired path, or use JQuery: $(‘body’).replaceWith(data) when you receive the HTML back from … Read more

What is process.env.PORT in Node.js?

In many environments (e.g. Heroku), and as a convention, you can set the environment variable PORT to tell your web server what port to listen on. So process.env.PORT || 3000 means: whatever is in the environment variable PORT, or 3000 if there’s nothing there. So you pass that to app.listen, or to app.set(‘port’, …), and that makes your server able to … Read more

Error: Cannot find module ‘ejs’

I had this exact same problem a couple of days ago and couldn’t figure it out. Haven’t managed to fix the problem properly but this works as a temporary fix: Go up one level (above app.js) and do npm install ejs. It will create a new node_modules folder and Express should find the module then.

Express Render not working return error: No default engine was specified and no extension was provided

I’m writing an Express application without a template engine I am using just using HTML as the template engine. app.set(‘view engine’, ‘html’); actually, the whole code was generated using express-generator and I set the view to –no-view Flag and the index URL page runs well but trying another URL like users or any other except the index URL does … Read more