Created
November 19, 2022 22:26
-
-
Save takehiko/a814f7651b93c2f2fc0f32c93f94706b to your computer and use it in GitHub Desktop.
A tiny HTTP server which accepts GET/POST methods with parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// server.js | |
// パラメータ付きのGET/POSTメソッドを受け付ける小さなHTTPサーバ | |
// 実行方法 | |
// 適当なディレクトリにこのファイルを置いて | |
// npm init -y | |
// npm install express | |
// node server.js | |
// 別端末で | |
// curl -X GET 'http://localhost:8080/?abc=def' | |
// curl -d ABC=xyz 'http://localhost:8080/' | |
const url = require("url"); | |
const express = require("express"); | |
const port = 8080; | |
const app = express(); | |
const server = app.listen(port, () => { | |
console.log("server listening..."); | |
app.use(express.json()) | |
app.use(express.urlencoded({ extended: true })); | |
app.use((req, res, next) => { | |
const u = url.parse(req.url, true); | |
u.query = { ...req.body, ...u.query }; | |
console.log({u}); | |
res.send(`Hello World (${req.method})\n`); | |
next(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment