Skip to content

Instantly share code, notes, and snippets.

@w5151381guy
Last active January 18, 2018 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w5151381guy/6c1db719c9e2a2f2d86b3f36848982e2 to your computer and use it in GitHub Desktop.
Save w5151381guy/6c1db719c9e2a2f2d86b3f36848982e2 to your computer and use it in GitHub Desktop.

用 Express 框架寫一個 Hello World server

提醒:Node.js 的模組採用的是 CommonJS 規範,所以不能寫 import xxx from 'xxx',要寫 const xxx = require('xxx')。除此之外,因為 Nodejs 更新很快,所以只要是符合 ES 標準的語法都可以使用,不用 babel 轉譯。

Express 是 Nodejs 目前最流行的 web 框架,可以很簡單的架起 API server,試著用 express 架一個 server 監聽 port 3000,直接在 nodejs-basic-practice 裡面建一個 002 的專案,完成以下幾個 API。

GET /api/testing

Response:

"Hello World GET"

POST /api/testing

Response

"Hello World POST"

寫完可以用 Postman 或是 curl 之類的工具測測看

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment