user7560350
user7560350

Reputation:

How to pass over an argument with POST method? NodeJS

So, as you may see I was the creator of the question "I am having this problem by passing over GET method". But now having kind of a problem with "Passing over with POST method" Here is my code to see what is going wrong. All I want to do is to print to say : "Hello (Whatever the user pass over name of).. If ExpressJS, doesn't work, can anyone show me in Javascript way?!

Here is the code.

    var server = require('./server');
var router = require('./router');
var requestHandlers = require('./requestHandlers');


 var handle = {
  '/': requestHandlers.start,
  '/start': requestHandlers.start,
  '/upload': requestHandlers.upload,
  '/show': requestHandlers.show
};

var express = require('express')
var app = express()


app.post('/view/users/:name', function(req, res) {
    console.log(req.body.desc);
    res.end();
});

app.listen(8080, function () {
  console.log('listening on port 8000!')
}) 

The error I get when passing over is "Cannot GET /view/users/John"

Upvotes: 0

Views: 50

Answers (2)

El houcine bougarfaoui
El houcine bougarfaoui

Reputation: 37343

you can access the path variable :name from req.params object

app.get('/view/users/:name', function(req, res) {
    console.log(req.params.name);
    res.end();
});

Upvotes: 2

Sadok Mtir
Sadok Mtir

Reputation: 615

You need to add bodyParser before your routes:

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

and then whatever you pass to the route, bodyParser will make it available within the request object.

Upvotes: 1

Related Questions