Express 4 and custom modules
I have the 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 ask /default/page
I get a response json
but the headers are not set (as stated in app.js). If I change default.js
and add the following lines I 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 that very soon I will have more than 10-15 modules and copying this code in each module does not seem correct. Please let me know why the header setting is app.js
not working.
EDIT1:
Here's the CURL output from app.use
inapp.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
to 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
source to share
Figured out the reason (silly to me), I used a sample project generated by an express generator that had a lot of middleware to generate a 404 and 500 response (using the default jade engine). In my case, I always wanted to receive JSON. I did the following steps and it worked
- Commented out all calls to res.render.
- Registered middleware before calling
app.use
.
Now the code 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);
It used to be in reverse order.
source to share