cd firstapp
touch index.js
npm install express
npm i -g nodemon (nodemon to auto-restart server)


const express = require("express")
const app = express();
app.listen(8080, () => {
console.log("this is 8080 port")
})
app.get(‘/‘, (req, res) => {
res.send(‘welcome to the home page~‘)
})
app.get(‘/cat‘, (req, res) => {
res.send(‘meow~‘)
})
app.post(‘/han‘, (req, res) => {
res.send(‘Lieeee‘)
}). //open postman, choose post setting, then sent, get Lieeee.
app.get(‘/dog‘, (req, res) => {
res.send(‘woof~‘)
})
//set multiple parameters
app.get(‘/r/:subreddit/:postID‘, (req, res) => {
const { subreddit, postID } = req.params;
res.send(`<h1>Viewing Post ID: ${postID} on the ${subreddit} subreddit</h1>`)
})
//working with query strings
app.get(‘/search‘, (req, res) => {
const { q } = req.query;
if (!q) {
res.send(‘NOTHING FOUND IF NOTHING SEARCHED!‘)
} else {
res.send(`<h1>Search results for: ${q}</h1>`)
}
})
// if can not find matching routing
app.get(‘*‘, (req, res) => {
res.send("I don‘t know the path~")
})





原文:https://www.cnblogs.com/LilyLiya/p/14360103.html