admin 管理员组

文章数量: 887021

快速

我是新来的JavaScript,并想检索我的快递航线GET请求的JSON响应。我可以返回整个JSON但是当我尝试返回一个特定元素我收到一个错误(如res.body.data)。这里是我的路由文件

route.js

var express = require('express');
var router = express.Router();
var request = require('request');
router.get('/', function(req, res, next) {
  request({
    uri: '/.json'
  }).pipe(res);
});
module.exports = router;

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require("cors");
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var testAPIRouter = require('./routes/testAPI');
var route = require('./routes/route');

var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(cors());
app.use(logger('dev'));
app.use(express.json());

app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/testAPI', testAPIRouter);
app.use('/route', route);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
module.exports = app;
回答如下:

你需要用身体解析器:

const app = express();
const bodyParser = require('body-parser'); 
app.use(bodyParser.urlencoded({ extended: false }));

本文标签: 快速