search
LoginSignup
3
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

【Node.js(Express4)】POSTデータの受け取り

はじめに

Express4.16以降からPOSTデータの受け取りにbody-parserをわざわざ使わなくてもよくなったらしい。
本記事では、フォームからのPOSTデータの送信を想定している。

body-parser使う版

command
$ npm install body-parser --save
index.js
var express = require('express');
var bodyParser = require('body-parser');
var app = express();

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

app.post('/', function (req, res) {} =>{
  res.json({
    msg: 'Hello World!',
    data: req.body
  });
});

Express4.16以降のやつ

index.js
var express = require('express');
var app = express();

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

app.post('/', function (req, res) {
  res.json({
    msg: 'Hello',
    data: req.body
  })
})

参考サイト

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
3
Help us understand the problem. What are the problem?