How to populate a sub-document in mongoose after creating it?

I am adding a comment to an item.comments list. I need to get the comment.created_by user data before I output it in the response. How should I do this? Item.findById(req.param(‘itemid’), function(err, item){ var comment = item.comments.create({ body: req.body.body , created_by: logged_in_user }); item.comments.push(comment);, item){ res.json({ status: ‘success’, message: “You have commented on this item”, […]

In Node.js, how do I “include” functions from my other files?

Let’s say I have a file called app.js. Pretty simple: var express = require(‘express’); var app = express.createServer(); app.set(‘views’, __dirname + ‘/views’); app.set(‘view engine’, ‘ejs’); app.get(‘/’, function(req, res){ res.render(‘index’, {locals: { title: ‘NowJS + Express Example’ }}); }); app.listen(8080); What if I have a functions inside “tools.js”. How would I import them to use in […]

Get hostname of current request in node.js Express

So, I may be missing something simple here, but I can’t seem to find a way to get the hostname that a request object I’m sending a response to was requested from. Is it possible to figure out what hostname the user is currently visiting from node.js?

Upload Base64 Image Facebook Graph API

I’m trying to upload a base64 image to a FaceBook page using Node.js. I have managed to get the upload working with all the multipart data etc should I read the file from the filesystem (ie. using fs.readFileSync(‘c:\a.jpg’) However, should I use the base64 encoded image and try upload it, it give me the following […]

What is Node.js' Connect, Express and “middleware”?

Despite knowing JavaScript quite well, I’m confused what exactly these three projects in Node.js ecosystem do. Is it something like Rails’ Rack? Can someone please explain?

Unknown events in nodejs/v8 flamegraph using perf_events

I try to do some nodejs profiling using Linux perf_events as described by Brendan Gregg here. Workflow is following: run node >0.11.13 with –perf-basic-prof, which creates /tmp/perf-(PID).map file where JavaScript symbol mapping are written. Capture stacks using perf record -F 99 -p `pgrep -n node` -g — sleep 30 Fold stacks using script from […]

How to make an HTTP POST request in node.js?

How can I make an outbound HTTP POST request, with data, in node.js?

When to close MongoDB database connection in Nodejs

Working with Nodejs and MongoDB through Node MongoDB native driver. Need to retrieve some documents, and make modification, then save them right back. This is an example: (err, db) { db.collection(‘foo’, function (err, collection) { var cursor = collection.find({}); cursor.each(function (err, doc) { if (doc != null) { doc.newkey = ‘foo’; // Make some […]

Basic Ajax send/receive with node.js

So I’m trying to make a very basic node.js server that with take in a request for a string, randomly select one from an array and return the selected string. Unfortunately I’m running into a few problems. Here’s the front end: function newGame() { guessCnt=0; guess=””; server(); displayHash(); displayGuessStr(); displayGuessCnt(); } function server() { xmlhttp […]

How to serve an image using nodejs

I have a logo that is residing at the public/images/logo.gif . Here is my nodejs code. http.createServer(function(req, res){ res.writeHead(200, {‘Content-Type’: ‘text/plain’ }); res.end(‘Hello World \n’); }).listen(8080, ‘’); It works but when I request for localhost:8080/logo.gif then I obviously I don’t get the logo. What changes I need to do to serve an image.

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