Express static routing resources

express route

To be mainly through the following method to distinguish url, whereby each different request processing module

  • app.get('/', (req,res)=>{res.send()})
  • app.post('/', (req,res)=>{res.send()})
  • app.pust('/', (req,res)=>{res.send()})
  • app.delete('/', (req,res)=>{res.send()})
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('hello world');// res.write + res.end
})

app.post('/', (req, res) => {
  res.write('post hello world');
  res.end()
})

app.put('/', (req, res) => {
  res.send('put hello world')
})

app.delete('/', (req, res) => {
  res.send('delete hello world')
})

app.listen(3000, () => {
  console.log('localhost start 3000 ...')
})

Express route static resource files

  • Static resource directory specified by express.static (sourceUrl)
  • By app.use () to complete the routing configuration
const express = require('express');
const path = require('path');
const app = express();

//配置多个静态资源目录
//访问:http://localhost:3000/loading.gif
app.use(express.static('public/images'));
// app.use(express.static('public/files'));

//配置多级目录
//访问:http://localhost:3000/static/1.png
app.use("/static",express.static('public/images'))

//配置绝对路径
//访问:http://localhost:3000/static/loading.gif
app.use('/static',express.static(path.resolve(__dirname,'public/files')));

app.listen(3000,()=>{
    console.log("server run at port 3000...");
});

effect

Single-level directory

Multi-level directory

Absolute path

Guess you like

Origin www.cnblogs.com/ltfxy/p/12528509.html