0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Node.js+Express】HTTP通信 POST受信

0
Posted at

毎回、忘れるので備忘録

初期設定

$ npm init
$ npm i express

サーバー側のプログラム

app.js
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

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

app.listen(3000);
console.log('Server is online.');

app.post('/', function(req, res) {
    // リクエストボディを出力
    console.log(req.body);
    // パラメータ名、nameを出力
    console.log(req.body.name);

    res.send('POST request to the homepage');
})

サーバーを立ち上げて

node app.js

ターミナルウィンドウをもう1つ開いて
以下curlで叩く

$ curl -X POST http://localhost:3000 -H "Accept: application/json" -H "Content-type: application/json" -d '{ "name" : "tatsuya" }'

VSCodeだとこんな感じ
スクリーンショット 2023-11-13 20.35.15.png



出力結果 ( node app.js の方)

{ name: 'tatsuya' }
tatsuya

以上です。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?