gpt4 book ai didi

javascript - Express 4 和自定义模块

转载 作者:搜寻专家 更新时间:2023-11-01 00:01:27 25 4
gpt4 key购买 nike

我在 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();
});

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;

当我请求 /default/page 时,我得到了 json 响应,但未设置 header (如 app.js 中所配置)。如果我修改 default.js 并添加以下行,我可以看到正在设置 header

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();
});

问题是,很快我就会有超过 10-15 个模块,并且在每个模块中复制这段代码,这看起来不是有效的选择。请告诉我为什么在 app.js 中设置 header 不起作用。

编辑 1:这是在 app.js

中使用 app.use 的 CURL 输出
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

在 default.js 中添加 router.use 后,输出如下

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

最佳答案

找出原因(愚蠢的我),我正在使用由 express generator 生成的示例项目,它有很多中间件来生成 404 和 500 响应(使用默认的 jade 引擎)。就我而言,我总是想要 JSON 输出。我执行了以下步骤并且有效

  1. 评论了所有 res.render 调用。
  2. 在调用 app.use 之前注册中间件。

现在的代码是这样的

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);

早些时候它是相反的顺序。

关于javascript - Express 4 和自定义模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com