Express Web Hello World
Express 是最小又靈活的 Node.js Web 應用程式架構,為 Web 與行動式應用程式提供一組健全的特性。
1. 安裝 express 套件
1
| npm install express --save
|
2. 新增 一個 app.js
1 2 3 4 5 6 7
| const express = require('express') const app = express() const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log('Example app listening on port ${port}!'))
|
3. Run node

4. Hello World!

RestfulAPI 實作 Get / Post / Put / Delete
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| const express = require('express') const app = express() const port = 3000
//// RestfulAPI
// get app.get('/myget', (req, res) => res.send('Hello World myget!') )
// post app.post('/mypost', function (req, res) { res.json({ RestfulAPI: 'mypost' }) })
// put app.put('/myput', function (req, res) { res.json({ RestfulAPI: 'myput' }) })
// delete app.delete('/mydelete', function (req, res) { res.json({ RestfulAPI: 'mydelete' }) })
|
Get
http://localhost:3000/myget

Post
http://localhost:3000/mypost

Put
http://localhost:3000/myput

Delete
http://localhost:3000/mydelete

1 2 3 4 5 6 7 8
| const express = require('express') const app = express() const port = 3000
//// static files app.use(express.static('static'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
|

參考