npm install express
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello, World!'); }); app.listen(3000, () => { console.log('Server is listening on port 3000'); });
node app.js
app.get('/', (req, res) => { res.send('Hello, World!'); });
app.get('/users/:id', (req, res) => { const userId = req.params.id; res.send('User ID: ' + userId); });
app.get('/users', (req, res) => { const name = req.query.name; res.send('Hello, ' + name); });
const bodyParser = require('body-parser'); app.use(bodyParser.json()); app.post('/users', (req, res) => { const user = req.body; res.send('Hello, ' + user.name); });
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com