Express - 无法在调试中提供静态文件

时间:2016-10-12 12:29:22

标签: node.js express

我无法在调试模式下提供静态文件。我正在使用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
    }
]

}

1 个答案:

答案 0 :(得分:0)

我也有这个问题。对我来说,工作目录设置不正确。将其设置到包含我的静态根目录的文件夹即可。您可以尝试以下方法:

"cwd": "${workspaceRoot}/bin/www",