Express-GET API始终返回200

我写了一个epoll_wait,它打印了传递的参数:

GET API

这是exports.agency_information = (req,res,next) => { console.log({param: req.params.usercode}); };

router

但是我并不理解为什么当我打电话给const router = require('express').Router(); const AgencyController = require('../controllers/agency'); router.get('/:usercode',AgencyController.agency_information); module.exports = router; 时总是返回api而在邮递员中这就是返回值:

200 OK

有或没有任何参数,结果都是相同的。

服务器代码

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta name="description" content="Fare la constatazione amichevole non è mai stato così facile" />
    <link rel="apple-touch-icon" href="logo192.png" />
    <link rel="manifest" href="/manifest.json" />
    <title>ConstaFAST</title>
    <link href="/static/css/2.0624914a.chunk.css" rel="stylesheet">
    <link href="/static/css/main.02a2fc0a.chunk.css" rel="stylesheet">
</head>

<body><noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <script>
        !function(i){function e(e){for(var r,t,n=e[0],o=e[1],u=e[2],l=0,f=[];l<n.length;l++)t=n[l],Object.prototype.hasOwnProperty.call(p,t)&&p[t]&&f.push(p[t][0]),p[t]=0;for(r in o)Object.prototype.hasOwnProperty.call(o,r)&&(i[r]=o[r]);for(s&&s(e);f.length;)f.shift()();return c.push.apply(c,u||[]),a()}function a(){for(var e,r=0;r<c.length;r++){for(var t=c[r],n=!0,o=1;o<t.length;o++){var u=t[o];0!==p[u]&&(n=!1)}n&&(c.splice(r--,1),e=l(l.s=t[0]))}return e}var t={},p={1:0},c=[];function l(e){if(t[e])return t[e].exports;var r=t[e]={i:e,l:!1,exports:{}};return i[e].call(r.exports,r,r.exports,l),r.l=!0,r.exports}l.m=i,l.c=t,l.d=function(e,t){l.o(e,r)||Object.defineProperty(e,{enumerable:!0,get:t})},l.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},l.t=function(r,e){if(1&e&&(r=l(r)),8&e)return r;if(4&e&&"object"==typeof r&&r&&r.__esModule)return r;var t=Object.create(null);if(l.r(t),Object.defineProperty(t,"default",value:r}),2&e&&"string"!=typeof r)for(var n in r)l.d(t,n,function(e){return r[e]}.bind(null,n));return t},l.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return l.d(r,"a",r),r},l.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},l.p="/";var r=this.webpackJsonpwww=this.webpackJsonpwww||[],n=r.push.bind(r);r.push=e,r=r.slice();for(var o=0;o<r.length;o++)e(r[o]);var s=n;a()}([])
    </script>
    <script src="/static/js/2.9ed2bde4.chunk.js"></script>
    <script src="/static/js/main.ea8659b8.chunk.js"></script>
</body>

</html>
abc123321tang 回答:Express-GET API始终返回200

首先从服务器发送响应。

exports.agency_information = (req,res,next) => {
    console.log({param: req.params.usercode});
    res.send('send something from server');
};

现在邮递员叫您的api

例如使用此

http://localhost:3000/api/agency/1234
  

在此处:将您的端口号替换为服务器上的端口   在跑。而1122是您要发送的参数   在控制台中打印。

如果这是您的默认路由,则将其调用,您会在邮递员中看到响应,并在控制台中看到参数打印。

更新您的服务器代码将api路由上移app.get(*)必须位于最后。因此,您将在邮递员中获取html页面。

这是更新的服务器代码

const express = require('express');
const app = express();
const path = require('path');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
require('dotenv').config()

const agencyRoutes = require('./api/routes/agency');
const companyRoutes = require('./api/routes/company');

const port = process.env.PORT || 3000;

mongoose.connect(process.env.DB_CONNECT,{
  useCreateIndex: true,useNewUrlParser: true,useUnifiedTopology: true
},() => console.log('Connect to the database'));

mongoose.Promise = global.Promise;

app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use( (req,next) => {
  res.header('Access-Control-Allow-Origin','*');
  res.header(
    'Access-Control-Allow-Headers','Origin,X-Requested-With,Content-Type,Accept,Authorization'
  );

  if ( req.method === 'OPTIONS' ) {
    res.header('Access-Control-Allow-Methods','PUT,POST,PATCH,DELETE,GET');
    return res.status(200).json({});
  }

  next();
});

app.use('/api/agency',agencyRoutes);
app.use('/api/company',companyRoutes);

app.use(express.static(path.resolve(__dirname,'../www','build')));
app.get('*',(req,res) => {
  res.sendFile(path.resolve(__dirname,'build','index.html'));
});

app.use( (req,next) => {
  const error = new Error('Not found');
  error.status = 404;
  next(error);
});

app.use( (error,req,next) => {
  res.status(error.status || 500);
  res.json({
    error: {
      message: error.message
    }
  });
});

app.listen(port,() => console.log(`Server up and running on port ${port}`));
,

这似乎是您的应用程序的默认页面,即使未传递任何内容。您说无论参数如何,您的输出都是相同的。似乎您无法输出默认页面。这是有道理的,因为意大利语说“ Fare la constatazione amichevole nonèmai statocosìfacile”做出友好的陈述从未如此简单。我将检查默认设置,以检查是否可以允许Java脚本,然后检查其是否有效。

因为这是JavaScript,所以您可能会阻止Java脚本允许其工作,并且这可能是Java脚本不工作时的默认页面。

exports.agency_information = (req,next) => {
  console.log({param: req.params.usercode});
};

您可以尝试使用来检查js是否也被禁用。

How to check javascript is enabled or not in Node JS server side code     <noscript> <meta http-equiv=refresh content='0; url=http://your.domain/noscript' </noscript>

本文链接:https://www.f2er.com/3141322.html

大家都在问