How to get all registered routes in Express?

I have a web application built using Node.js and Express. Now I would like to list all registered routes with their appropriate methods. E.g., if I have executed app.get(“https://stackoverflow.com/”, function (…) { … }); app.get(‘/foo/:id’, function (…) { … }); app.post(‘/foo/:id’, function (…) { … }); I would like to retrieve an object (or something … Read more

Purpose of installing Twitter Bootstrap through npm?

Question 1: What exactly is the purpose of installing Twitter Bootstrap through npm? I thought npm was meant for server side modules. Is it faster to serve the bootstrap files yourself than using a CDN? Question 2: If I were to npm install Bootstrap, how would I point to the bootstrap.js and bootstrap.css files? 4 … Read more

What is NODE_ENV and how to use it in Express?

This is my app that I’m currently running on production. var app = express(); app.set(‘views’,settings.c.WEB_PATH + ‘/public/templates’); app.set(‘view engine’,’ejs’); app.configure(function(){ app.use(express.favicon()); app.use(express.static(settings.c.WEB_PATH + ‘/public’)); app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.methodOverride()); app.use(express.session({ cookie:{ domain:”.”+settings.c.SITE_DOMAIN, maxAge:1440009999}, secret:’hamster’, store: r_store, })); app.use(useragent.express()); app.use(flash()); app.use(passport.initialize()); app.use(passport.session()); }); Now I’ve learned about NODE_ENV and want to use it. How can I do this? … Read more

static files with express.js

I want to serve index.html and /media subdirectory as static files. The index file should be served both at /index.html and / URLs. I have web_server.use(“/media”, express.static(__dirname + ‘/media’)); web_server.use(“https://stackoverflow.com/”, express.static(__dirname)); but the second line apparently serves the entire __dirname, including all files in it (not just index.html and media), which I don’t want. I … Read more

Difference between app.use and app.get in express.js

I’m kind of new to express and node.js, and I can’t figure out the difference between app.use and app.get. It seems like you can use both of them to send information. For example: app.use(“https://stackoverflow.com/”,function(req, res,next) { res.send(‘Hello’); next(); }); seems to be the same as this: app.get(“https://stackoverflow.com/”, function (req,res) { res.send(‘Hello’); }); 7 Answers 7

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

I have the following Node.js code: var express = require(‘express’); var app = express.createServer(express.logger()); app.use(express.bodyParser()); app.post(“https://stackoverflow.com/”, function(request, response) { response.write(request.body.user); response.end(); }); Now if I POST something like: curl -d user=Someone -H Accept:application/json –url http://localhost:5000 I get Someone as expected. Now, what if I want to get the full request body? I tried doing response.write(request.body) … Read more