Without further ado, let’s build a local service with Express
var express = require('express')
var path = require('path')
var app = express()
app.get('/', (req, res)=>{
res.send('Hello world');
});
app.listen(8083, () = > {console.log('Server is running at http://localhost:8083')})Copy the code
Then type Node app on the command line to start the service
At this time, we open the browser and visit http://localhost:8083 to see Hello World, indicating that the request is successful
app.get('/img'.function (req, res, next) {
res.sendFile(path.join(__dirname, 'public/2.png'));
})
Copy the code
This time we enter http://localhost:8083/img to the browser can access to the image
app.get('/air'.function (req, res, next) {
res.sendFile(path.join(__dirname, 'public/lenveo.mp4'));
})
Copy the code
The same we use browser open http://localhost:8083/air can access to the video
The complete code