LoginSignup
11
3

More than 3 years have passed since last update.

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

Posted at

はじめに

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
  })
})

参考サイト

11
3
0

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
  3. You can use dark theme
What you can do with signing up
11
3