Questions tagged [express]

Express.js is a minimal and flexible Node.js web application framework providing a robust set of features for building web applications.

Express.js is a minimal and flexible Node.js web application framework providing a robust set of features for building web applications.

The official website of Express.js is expressjs.com. The source can be found on GitHub.

Useful links:

Older versions

94248 questions
251
votes
13 answers

How to specify HTTP error code using Express.js?

I have tried: app.get('/', function(req, res, next) { var e = new Error('error message'); e.status = 400; next(e); }); and: app.get('/', function(req, res, next) { res.statusCode = 400; var e = new Error('error message'); …
tech-man
  • 3,166
  • 2
  • 17
  • 18
250
votes
22 answers

add created_at and updated_at fields to mongoose schemas

Is there a way to add created_at and updated_at fields to a mongoose schema, without having to pass them in everytime new MyModel() is called? The created_at field would be a date and only added when a document is created. The updated_at field would…
chovy
  • 72,281
  • 52
  • 227
  • 295
245
votes
9 answers

What does middleware and app.use actually mean in Expressjs?

Almost every Express app I see has an app.use statement for middleware but I haven't found a clear, concise explanation of what middleware actually is and what the app.use statement is doing. Even the express docs themselves are a bit vague on this.…
iZ.
  • 4,951
  • 5
  • 23
  • 16
243
votes
7 answers

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

what is process.env.PORT || 3000 used for in Node.js? I saw this somewhere: app.set('port', process.env.PORT || 3000); If it is used to set 3000 as the listening port, can I use this instead? app.listen(3000); If not why?
user-S
  • 3,019
  • 3
  • 16
  • 10
242
votes
4 answers

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…
James Fazio
  • 6,370
  • 9
  • 38
  • 47
240
votes
11 answers

how to get request path with express req object

I'm using express + node.js and I have a req object, the request in the browser is /account but when I log req.path I get '/' --- not '/account'. //auth required or redirect app.use('/account', function(req, res, next) { …
chovy
  • 72,281
  • 52
  • 227
  • 295
233
votes
13 answers

Proper way to set response status and JSON content in a REST API made with nodejs and express

I am playing around with Nodejs and express by building a small rest API. My question is, what is the good practice/best way to set the code status, as well as the response data? Let me explain with a little bit of code (I will not put the node and…
dukable
  • 3,968
  • 11
  • 31
  • 42
232
votes
6 answers

How to programmatically send a 404 response with Express/Node?

I want to simulate a 404 error on my Express/Node server. How can I do that?
Randomblue
  • 112,777
  • 145
  • 353
  • 547
229
votes
17 answers

How do I move files in node.js?

How can I move files (like mv command shell) on node.js? Is there any method for that or should I read a file, write to a new file and remove older file?
rizidoro
  • 13,073
  • 18
  • 59
  • 86
229
votes
23 answers

Automatic HTTPS connection/redirect with node.js/express

I've been trying to get HTTPS set up with a node.js project I'm working on. I've essentially followed the node.js documentation for this example: // curl -k https://localhost:8000/ var https = require('https'); var fs = require('fs'); var options…
Jake
  • 5,379
  • 6
  • 19
  • 19
227
votes
13 answers

HTTP GET Request in Node.js Express

How can I make an HTTP request from within Node.js or Express.js? I need to connect to another service. I am hoping the call is asynchronous and that the callback contains the remote server's response.
Travis Parks
  • 8,435
  • 12
  • 52
  • 85
221
votes
8 answers

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…
Jesse
  • 10,370
  • 10
  • 62
  • 81
219
votes
13 answers

Sending JWT token in the headers with Postman

I'm testing an implementation of JWT Token based security based off the following article. I have successfully received a token from the test server. I can't figure out how to have the Chrome POSTMAN REST Client program send the token in the…
Diode Dan
  • 4,801
  • 6
  • 25
  • 34
218
votes
12 answers

Proxy with express.js

To avoid same-domain AJAX issues, I want my node.js web server to forward all requests from URL /api/BLABLA to another server, for example other_domain.com:3000/BLABLA, and return to user the same thing that this remote server returned,…
user124114
  • 8,372
  • 11
  • 41
  • 63
217
votes
7 answers

Node.js global variables

I asked here: Does Node.js require inheritance? And I was told that I can set variables to the global scope by leaving out the variable. This does not work for me. That is, the following does not make the _ available on required files. _ =…
Harry
  • 52,711
  • 71
  • 177
  • 261