Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
nodejs server not receiving any parameters from POST request

server.js:

    var express = require('express');
    var app = express();
    loggedIn = {};

    app.use('/',express.static('www')); // static files

    app.listen(8080, function () {
      console.log('Port 8080!');
    });

    app.get('/user', function(req, res) {
        if (typeof req.param('user') != 'undefined') {
            user = req.param('user');
            res.status(200).send('Works');
        }
    });

    app.post('/user', function(req, res) {
            user = req.param('user');
            if (typeof users[user] != 'undefined') {
                return res.status(405).send('Access Forbidden');
            } else {
                   loggedIn[user] = "";
                   res.status(201).send('New User');
            }
        }
    });

client.js requests:

    $.ajax({
        method: "GET",
        url: "/user",
        data: {"user" : user},
        dataType: "application/json",
        success: function(data) {
            // success
        },
        error: function() {
            // error case
        }
    });

    $.ajax({
        method: "POST",
        url: "/user",
        data: {"user" : user},
        dataType: "application/json",
        success: function(data) {
            // success
        },
        error: function() {
            // error case
        }
    });

Even though the GET request works exactly as expected and passes the parameter here, for some reason, the post request doesn't. In firebug, I notice the POST request receives no parameters whatsoever (POST user) while GET request does (GET user?user=XYZ). I am really at a loss right now.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have to tell your express app to parse the request body

app.use(express.bodyParser());

for express 4+

var express    = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse application/json
app.use(bodyParser.json())

For reference goto https://expressjs.com/en/4x/api.html#req and look at the section titled req.body

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!