首页 > 解决方案 > 使用 expressjs 提供静态文件

问题描述

这是我的代码

I want to hide my folder from client and put alias before file name

const express = require('express');
const path = require('path');
const app = express();
app.use('/public',express.static(path.join(__dirname,'static')));
app.get('/',(req,res)=> {
    res.sendFile(path.join(__dirname,'static','index.html'));
}).listen(3000);

在 chrome 上检查时,它应该给出:

<link rel='stylesheet' href='/public/css/main.css'>
<script src='/public/js/example.js'></script>

But it is giving me:

   <link rel='stylesheet' href='main.css'>
   <script src='example.js'></script>

标签: javascriptnode.jsexpresshttp-postmiddleware

解决方案


推荐阅读