用这句代码
1 |
app.get('/', (req, res) => res.json({ answer: 42 })); |
具体实现为:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => res.send('Hello World!')) app.get('/countrys', (req, res) => res.json({ "countrys": { "thailand": { "url": "http://www.bejson.com", "situations": [ { "name": "at airport", "url": "http://www.google.com" }, { "name": "rail station", "url": "http://www.baidu.com" }, { "name": "taxi", "url": "http://www.SoSo.com" } ] }, "laos": { "url": "http://www.bejson.com", "situations": [ { "name": "at airport", "url": "http://www.google.com" }, { "name": "rail station", "url": "http://www.baidu.com" }, { "name": "taxi", "url": "http://www.SoSo.com" } ] } } } )); app.listen(port, () => console.log(`Example app listening on port ${port}!`)) |
执行 node app.js
当访问 http://IP:3000/countrys , 会返回正确的json字符串
参考文章
https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express
https://www.youtube.com/watch?v=EwHMSxSWIvQ
这个教程非常好+
图片都存在 imgbb,google 账号登陆