Luc*_*osa 5 javascript node.js express
我浏览了很多其他帖子,但我对此感到非常迷失。
我可以运行 aconsole.log(req)并得到以下输出
ServerResponse {
...
req:
IncomingMessage {
...
url: '/my-endpoint',
method: 'POST',
statusCode: null,
statusMessage: null,
...
body: { foo: 'bar' },
_body: true,
...
route: Route { path: '/my-endpoint', stack: [Object], methods: [Object] } },
...
Run Code Online (Sandbox Code Playgroud)
看起来很可靠,所以我希望这样做console.log(req.body)并返回{ foo: 'bar' }控制台......但是不,变得未定义
经过研究,我发现这可能与我的app.js文件有关,特别是与body-parser,但是,我已经拥有了所有这些
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//Home Page Rendering
var index = require('./routes/index');
app.use('/', index);
// All other routes are kept in the `routes` module
require('./routes')(app);
module.exports = app;
Run Code Online (Sandbox Code Playgroud)
routes.js
module.exports = function routing(app) {
var apiClient = require('./services/apiClient');
app.post('/get-device-info', function(err, req, res){
console.log("/get-device-info routes");
apiClient(req, res);
});
};
Run Code Online (Sandbox Code Playgroud)
apiClient.js
module.exports = function callApi(req, res){
console.log(req);
console.log(req.body)
};
Run Code Online (Sandbox Code Playgroud)
index.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的
app.use(express.bodyParser());
确保传入请求显式声明 application/json
以其他方式声明 body parser.json
添加配置功能
您的问题是 Express 不对其路由处理程序使用错误优先回调。下面的代码将不起作用,因为app.post处理程序没有签名(req, res) => {}。在下面的代码中err等于req、req等于res、并且res等于next。
// routes.js
module.exports = function routing(app) {
var apiClient = require('./services/apiClient');
app.post('/get-device-info', function(err, req, res){
console.log("/get-device-info routes");
// Assuming the request is correct
// the value of body will be the request body
console.log(res.body)
apiClient(req, res);
});
};`
Run Code Online (Sandbox Code Playgroud)
您可以在 Express 中使用 3 种不同的路由回调签名
(req, res) => {}- 这是最基本的,定义了一个只关心请求和响应的路由(req, res, next) => {}- 这是中间件回调签名,看起来像基本的路由回调,但它还分配next告诉 Express 调用下一个匹配路由的对象。(err, req, res, next) => {}- 这是错误处理路由回调,每个 Express Router 中间件或 Express 应用程序只需要 1 个。next(err)如果在路由或中间件内调用它,则会调用它。更具体地说,如果next()不使用字符串或什么都不调用,它将跳过所有剩余的中间件和路由并直接进入错误处理程序。您可以在Express 文档中阅读更多相关信息您需要将路线定义更改为
app.post('/get-device-info', (req, res) => {
apiClient(req, res)
})
Run Code Online (Sandbox Code Playgroud)
或者你甚至可以这样做
module.exports = app => {
let apiClient = require('./services/apiClient')
app.post('/get-device-info', apiClient)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10961 次 |
| 最近记录: |