NodeJS-允许公众访问“ public”文件夹

时间:2020-04-21 16:32:31

标签: node.js

我正在尝试公开访问NodeJS中的文件夹。

这是我的app.js

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const session = require('express-session');

//const con = require('./database');
const errorController = require('./controllers/error');

const app = express(); // express framework starts

// Use the view engine => EJS
app.set('view engine', 'ejs');
app.set('views', 'views');

const homeRouter = require('./routes/home');
const authRouter = require('./routes/auth');

app.use(bodyParser.urlencoded({extended: false}));
// give public access to public folder
app.use(express.static(path.join(__dirname, 'public')));
// Initialize the session
app.use(session({
    secret: 'mySecret',
    resave: false,
    saveUninitialized: false,
    rolling: true,
    cookie: {
        expires: 1000*60
    }
}));

app.use(homeRouter);
app.use(authRouter);

// If nothing was found (Wrong URL)
//app.use(errorController.get404);

app.listen(3000);

当我尝试访问:http://localhost:3000/public/css/login.css时,它返回:无法获取/public/css/login.css

我想念什么吗?

谢谢

1 个答案:

答案 0 :(得分:1)

是的,URL中省略了公共目录,然后尝试:

http://localhost:3000/css/login.css