Express - 无法在调试模式下提供静态文件服务

3

调试模式下,我无法提供静态文件服务。我正在使用Passport身份验证。Index.html是登录页面,其中包含表单(POST到/login)。在调试模式下,成功验证后服务器重定向到/home,但我无法获取我的css和javascript文件。

这些文件位于/public目录中。

/public
   /css
   /javascripts
   /images
   index.html
   home.html
/routes

在 app.js 文件中:
app.use(express.static(path.join(__dirname, 'public')));

在 routes/index.js 中

/* GET Home Page */
router.get('/home', isAuthenticated, function(req, res){
    var root = join(__dirname, '/..');
    root = join(root, '/public/');
    res.sendFile('home.html', { root: root});
});
/* Handle Login POST */
    router.post('/login', passport.authenticate('login', {
        successRedirect: '/home',
        failureRedirect: '/',
        failureFlash : true  
    }));

我正在使用VS Code。这是我的launch.json文件。

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Launch",
        "type": "node",
        "request": "launch",
        "program": "${workspaceRoot}/bin/www",
        "stopOnEntry": false,
        "args": [],
        "cwd": "${workspaceRoot}",
        "preLaunchTask": null,
        "runtimeExecutable": null,
        "runtimeArgs": [
            "--nolazy"
        ],
        "env": {
            "NODE_ENV": "development"
        },
        "console": "internalConsole",
        "sourceMaps": false,
        "outDir": null
    },
    {
        "name": "Attach",
        "type": "node",
        "request": "attach",
        "port": 5858,
        "address": "localhost",
        "restart": false,
        "sourceMaps": false,
        "outDir": null,
        "localRoot": "${workspaceRoot}",
        "remoteRoot": null
    },
    {
        "name": "Attach to Process",
        "type": "node",
        "request": "attach",
        "processId": "${command.PickProcess}",
        "port": 5858,
        "sourceMaps": false,
        "outDir": null
    }
]

}


你是如何加载你的 .css 和 .js 文件的? - RigidBody
在 HTML 文件中使用 <link> 和 <script>: - miuosh
materialize.min.js和materialize.min.css文件存储在哪里? - RigidBody
请记住,您将app.use...设置为“public”。 - RigidBody
materialize文件夹位于public目录下。一切都很完美,直到我在VS Code的调试模式下启动应用程序。在'/'目录中,应用程序加载css、js文件等。然后我输入凭据(用户名、密码),在成功授权后,应用程序从'/'重定向到'/home'。在'/home'目录中,我无法加载css、js文件(但仅在调试模式下)。 - miuosh
1个回答

1
我也遇到了这个问题。对我来说,问题在于工作目录没有设置正确。将其设置为包含静态根目录的文件夹即可解决。在你的情况下,你可以尝试:
"cwd": "${workspaceRoot}/bin/www",

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接