首页 > 解决方案 > 我怎样才能让 express.static() 函数最后忽略“/”?

问题描述

我正在制作一个服务器,用于呈现一个 html 页面

onst express = require('express');
const server = express();
const path = require('path');
//const WebSocket = require('ws');
const port = 8088;

server.use(express.static(path.join(__dirname, 'static')));
server.set('view engine',"ejs");
server.get('/emulator',(req,res)=> {
  console.log(req.url);
  res.render('app');
  res.end();
})

当我发送这个请求时它工作 http://localhost:8088/emulator 但是当我像这样 http://localhost:8088/emulator/ 最后添加“/”时,只有 html 页面出现,没有 css,js 这如何我在 html 页面中调用静态文件

<link rel="stylesheet" href="css/style.css">
<script src="js/main.js"></script>

标签: javascriptnode.jsexpress

解决方案


推荐阅读