Last active
September 15, 2017 04:10
-
-
Save butackle/766229cd550793b3ddb67ae3b167c946 to your computer and use it in GitHub Desktop.
20170906 express
This file contains hidden or 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
const express = require("express"); | |
const app = express(); | |
//使用するポートを指定する。 | |
const port = 8080; | |
const server = app.listen(port, () => { | |
// ポート設定時に実行される処理 | |
console.log(`Node.js is listening to PORT: ${ server.address().port }`); | |
}); | |
app.listen(2020); // 複数ポートの指定も可。 | |
// 指定したディレクトリのファイル配置にしたがって、指定したパスの下に表示させる | |
app.use('/', express.static(`files`)); | |
// 指定したパスのレスポンスを設定する | |
// POST・PUT・DELETEなども可。 | |
app.get("/get", (req, res) => { | |
// GET時に渡された値 | |
console.log(req.query); | |
const responseText = Object.keys(req.query) | |
.map((key) => `${ key } is ${ req.query[key] }`) | |
.join("<br />"); | |
// レスポンスを返す。 | |
res.send(responseText); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment