Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express - can't serve static files in debug

I cannot serve static files in debug mode. I am using Passport authentication. Index.html is login page with form (POST to /login). In debug mode after successful authentication server redirect to /home but I cannot GET my css and javascript files.

Files are in /public directory.

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

in app.js:

app.use(express.static(path.join(__dirname, 'public')));

in 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  
    }));

I am using VS Code. Here's 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
    }
]

}

like image 633
miuosh Avatar asked Jan 21 '26 17:01

miuosh


1 Answers

I had this problem too. For me it was that the working directory was not set correctly. Setting it to the folder containing my static root worked. In your case you might try:

"cwd": "${workspaceRoot}/bin/www",
like image 61
Chris Caulfield Avatar answered Jan 25 '26 12:01

Chris Caulfield