简体   繁体   English

Express req.query 适用于 GET 但不适用于 POST

[英]Express req.query works fine with GET but not with POST

I'm using nodejs with express and trying to use query parameters.我正在使用带有 express 的 nodejs 并尝试使用查询参数。 For some reason they work fine with GET but don't work at all with POST.出于某种原因,它们在 GET 上工作得很好,但在 POST 上根本不工作。

Here is my route file:这是我的路线文件:

var express = require('express');
var router = express.Router();

router.get('/get', function (req, res, next) {
    var schemaId = req.query.schemaId;
    console.log("GET: "+schemaId);
  });

router.post('/add', function (req, res, next) {
    var schemaId = req.query.schemaId;
    console.log("ADD: "+schemaId);
  });

and here is the app.js itself:这是 app.js 本身:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const dotenv = require('dotenv');

if (process.env.NODE_ENV !== 'production') {
  dotenv.config();
}

var indexRouter = require('./routes/index');
var schemasRouter = require('./routes/schemas');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/schemas',schemasRouter);

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

so doing localhost:3000/schemas/get?schemaId=test works just fine.所以localhost:3000/schemas/get?schemaId=test工作得很好。 "GET: test" is printed.打印“GET:测试”。

but doing localhost:3000/schemas/add?schemaId=test doesn't print anything in the console.但是做localhost:3000/schemas/add?schemaId=test不会在控制台中打印任何内容。

Seems like a super simple thing and all the examples I have seen successfully use res.query with POST.似乎是一件超级简单的事情,我看到的所有示例都成功地将 res.query 与 POST 一起使用。

but doing localhost:3000/schemas/add?schemaId=test doesn't print anything但做 localhost:3000/schemas/add?schemaId=test 不打印任何东西

Making a request via the browser's URL will always send a GET request and never a POST request.通过浏览器的 URL 发出请求将始终发送GET请求,而不是POST请求。

Either write a test html page like this:像这样编写测试 html 页面:

<html><script>
    fetch(url, {method: 'POST'})
        .then(res => res.text())
        .then(res => document.body.innerHTML += res);
</script></html>

Or use something like Postman或使用类似Postman

This..这个..

app.use('/schemas',schemasRouter);

And This...和这个...

var schemasRouter = require('./routes/schemas');

So maybe... This?所以也许……这个?

localhost:3000/routes/schemas/add?schemaId=test 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM