Nested if inside each (Handlebars template in Express app)

This morning I figured I’d finally take a look at Handlebars by throwing together a quick Express app. I’m enjoying it so far (and I think I prefer Handlebars to Jade) but I have a problem. The app I’m playing with is (yet another) basic CMS using Mongo. On the /posts/index page, I’m checking for […]

Why is ElasticSearch match query returning all results?

I have the following ElasticSearch query which I would think would return all matches on the email field where it equals myemails@email.com “query”: { “bool”: { “must”: [ { “match”: { “email”: “myemail@gmail.com” } } ] } } The mapping for the user type that is being searched is the following: { “users”: { “mappings”: […]

Error: Can't set headers after they are sent , happen only after refreshing a page

Every time i refresh a html page at route ‘localhost:8080/’ or login a user I will get this Error: Can’t set headers after they are sent. As far as I know this problem happens due to callbacks that are accidentally called twice. The app doesn’t crash or anything, its just keep logging the same error […]

Express 4.x compression

I have tested my local web server with Google’s PageSpeed chrome extension. One of the results was that my web server doesn’t have compression enabled. I am using node js on the backend with express 4.x. I googled a bit to find a solution to compress data and I found https://github.com/expressjs/compression a middleware for express. […]

Using Gulp to manage opening and closing MongoDB

So far I have a child_process that executes ‘mongod –dbpath db’ and another child_process which can kill it with ‘mongod –dbpath db –shutdown’. How do I listen for the user to enter ctrl-c or exit the gulp runner, and then run the gulp task to shutdown mongo?

Conditionally Include Dependency package.json

I have a dependency that is only needed for Mac OS in an npm project and was wondering if there is some way to conditionally include this dependency only when the compatible platform is the one running npm install. I’m willing to write the logic for this. In the below case grunt-appdmg is causing the […]

what will happen if I use socket.setKeepAlive in Node.js server

I just want to ask in net module of Node.js because I did not fully understand in the docs. what will happen if I implement the setKeepAlive() ?. what is the behavior of this setKeepAlive() ? var net = require(‘net’); var server = net.createServer(function(socket){ socket.setKeepAlive(true,60000); //1 min = 60000 milliseconds. socket.on(‘data’,function(data){ ///receiving data here }); […]

Visual Studio Code – debug by “Attach” does not work on Mac OS

I’m trying to debug my node js project on Mac. It works very well in “Launch” mode but not in “Attach” mode. In “Attach” mode, when I start debugging, I see VS Code first trying to start the debugger in seconds and then stop suddenly without showing any error message. I leave everything in their […]

Need to send response after forEach is done

I’m working with NodeJS + Mongoose and I’m trying to populate an array of objects and then send it to the client, but I can’t do it, response is always empty because it is sent before forEach ends. router.get(‘/’, isAuthenticated, function(req, res) { Order.find({ seller: req.session.passport.user }, function(err, orders) { //handle error var response = […]

node and express send json formatted

I’m trying to send formatted json with express. Here is my code: var app = express(); app.get(‘/’, function (req, res) { users.find({}).toArray(function(err, results){ // I have try both res.send(JSON.stringify(results, null, 4)); // OR res.json(results); }); }); I get the json in my browser but it’s a string. How can I send it so it’s readable […]

Node.js is the Best Javascript runtime in the world.