Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express 4 and custom modules

I have following code in app.js

var express = require('express');
....
var app = express();
var defaultPage = require('./routes/default');
app.use('/default', defaultPage);
...
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

and inside default.js

var express = require('express');
var router = express.Router();

router.get('/page', function(req, res, next) {
  res.send({
    pageTo:"app.entity", params:{"entityID":1}
  });
});

module.exports = router;

when I request /default/page, I get the json response, but the headers are not set (as configured in app.js). If I modify the default.js and add following lines, I can see the headers are being set

router.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

The problem is, very soon I will be having more than 10-15 modules and copying this code in every module, doesn't look valid option. Please let me know why setting header in app.js is not working.

EDIT1: Here goes the CURL output with app.use in app.js

D:\node_service>curl --verbose http://localhost:3000/default/page
* Adding handle: conn: 0x1e3cf48
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x1e3cf48) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 3000 (#0)
*   Trying ::1...
* Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /default/page HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 47
< ETag: W/"2f-1d1f551c"
< Date: Sun, 03 May 2015 13:44:30 GMT
< Connection: keep-alive
<
{"pageTo":"app.entity","params":{"entityID":1}}* Connection #0 to host localhost left intact

And after adding router.use in default.js, here is the output

D:\node_service>curl --verbose http://localhost:3000/default/page
* Adding handle: conn: 0x1e1cf48
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x1e1cf48) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 3000 (#0)
*   Trying ::1...
* Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /default/page HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
< Content-Type: application/json; charset=utf-8
< Content-Length: 47
< ETag: W/"2f-1d1f551c"
< Date: Sun, 03 May 2015 13:47:07 GMT
< Connection: keep-alive
<
{"pageTo":"app.entity","params":{"entityID":1}}* Connection #0 to host localhost left intact
like image 878
CuriousMind Avatar asked Sep 07 '26 16:09

CuriousMind


1 Answers

Figured out the reason (silly me), I was using the sample project generated by express generator, which had lot of middleware to generate 404 and 500 response (using default jade engine). In my case, I wanted JSON output always. I did following steps and it worked

  1. Commented all res.render calls.
  2. Registered middleware prior to calling app.use.

The code now looks like this

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.use('/default', defaultPage);

Earlier it was in reverse order.

like image 190
CuriousMind Avatar answered Sep 10 '26 05:09

CuriousMind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!