0

I'm using nodejs with express and trying to use query parameters. For some reason they work fine with GET but don't work at all with 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:

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. "GET: test" is printed.

but doing localhost:3000/schemas/add?schemaId=test doesn't print anything in the console.

Seems like a super simple thing and all the examples I have seen successfully use res.query with POST.

3
  • 1
    What are you using to do the POST request, you can’t just use a browsers url. Commented May 27, 2020 at 22:06
  • That is exactly what I was doing. I now remember you can't. It has been some time since I have done web dev, I'm bit rusty. Commented May 27, 2020 at 22:12
  • Try using routes/schemas/add?etc.. as that is what you are setting the route to. Commented May 28, 2020 at 2:45

2 Answers 2

2

but doing localhost:3000/schemas/add?schemaId=test doesn't print anything

Making a request via the browser's URL will always send a GET request and never a POST request.

Either write a test html page like this:

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

Or use something like Postman

Sign up to request clarification or add additional context in comments.

Comments

0

This ..

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

And This ...

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

So maybe... This ?

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.