nodejs express: Setting express.static path as parent folder (/..) error

import express from "express"
import path from "path"

const app = express();
const __dirname = path.resolve();

app.use(express.static(`${__dirname}/../'webapp_test`))   //ERROR

app.get(`/`,(req,res)=>{
    res.sendFile(`${__dirname}/../webapp_test/todo.html`);
});

app.listen(8080);

By setting express.static() path as string literal with escape sequence ${}

Error occurs when connect to localhost:8080 like

ForbiddenError: Forbidden
    at SendStream.error (WORKING DIRECTORY\node_modules\send\index.js:270:31)
    at SendStream.pipe (WORKING DIRECTORY\node_modules\send\index.js:553:12)
    at sendfile (WORKING DIRECTORY\node_modules\express\lib\response.js:1103:8)
    at ServerResponse.sendFile (WORKING DIRECTORY\node_modules\express\lib\response.js:433:3)
    at file://WORKING DIRECTORY/main.js:13:9
    at Layer.handle [as handle_request] (WORKING DIRECTORY\node_modules\express\lib\router\layer.js:95:5)
    at next (WORKING DIRECTORY\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (WORKING DIRECTORY\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (WORKING DIRECTORYt\node_modules\express\lib\router\layer.js:95:5)
    at WORKING DIRECTORY\node_modules\express\lib\router\index.js:281:22

But if I set path with path.join method as

app.use(express.static(path.join(__dirname,`..`,`webapp_test`));

app.get(`/`,(req,res)=>{
    res.sendFile(path.join(__dirname,`..`,`webapp_test`,`todo.html`));
});

Page loads well

What am I missing?



Read more here: https://stackoverflow.com/questions/65719641/nodejs-express-setting-express-static-path-as-parent-folder-error

Content Attribution

This content was originally published by co_lin at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: