1

I am working on node js server which use socket io.

var express = require('express') 
app = express.createServer();
app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Auth-Token");
    next();
 });
app.get("/", function(req, res) {res.send('admin')});

i need to listen to post request with json body so i've added these lines:

app.post('/testPost',function(req,res){

       console.log("post json : " + req.body );
});

but i am receiving undefined request body. that because i should add :

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

but now after adding bodyParser.json() the socket io doesn't connect !

what should i do ? how to ask node js to use bodyParser.urlencoded just with /testPost route ?

david
  • 3,310
  • 7
  • 36
  • 59

0 Answers0