Skip to content

Instantly share code, notes, and snippets.

@takehiko
Created November 19, 2022 22:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takehiko/a814f7651b93c2f2fc0f32c93f94706b to your computer and use it in GitHub Desktop.
Save takehiko/a814f7651b93c2f2fc0f32c93f94706b to your computer and use it in GitHub Desktop.
A tiny HTTP server which accepts GET/POST methods with parameters
// 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