Can't get index.html to show with Express in Nodejs

后端 未结 2 1615
情歌与酒
情歌与酒 2021-02-09 00:23

I\'m trying to run my first express app, but can\'t seem to get my webpage to show. I have the following code:

var fs = require(\"fs\");
var config = JSON.parse         


        
相关标签:
2条回答
  • 2021-02-09 01:17

    I had this issue .... after a lot of trouble I find that if you run two workspaces or project at t time then it will create this scenario. so you might open only a workspace at a time and not just file ... open the hole folder then run the specific file.make the following change in your VS code setting.

    setup the settings

    For 64bit system.

    {
        "liveServer.settings.AdvanceCustomBrowserCmdLine: ": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
        "liveServer.settings.NoBrowser": false
    }
    

    For 32bit system.

    {
        "liveServer.settings.AdvanceCustomBrowserCmdLine: ": "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
        "liveServer.settings.NoBrowser": false
    }
    
    0 讨论(0)
  • 2021-02-09 01:20

    Your app will only route page requests that are set up at the time of your app.use(app.router) call. So reorder your app.use calls to be one of the following:

    Express 3

    app.use(express.static(__dirname + "/public"));
    app.use(app.router);
    

    __dirname is the directory that the executing script resides in, so because that lives in the js directory that's a peer to public your code would need to be:

    app.use(express.static(__dirname + "/../public"));
    app.use(app.router);
    

    Express 4

    Express 4 removes the need to manually do app.use(app.router). With Express 4 you just need:

    app.use(express.static(__dirname + "/../public"));

    0 讨论(0)
提交回复
热议问题