Cannot GET / Nodejs Error

后端 未结 4 1206
面向向阳花
面向向阳花 2020-12-16 16:06

I\'m using the tutorial found here: http://addyosmani.github.io/backbone-fundamentals/#create-a-simple-web-server and added the following code.

// Module dep         


        
相关标签:
4条回答
  • 2020-12-16 16:37

    Much like leonardocsouza, I had the same problem. To clarify a bit, this is what my folder structure looked like when I ran node server.js

    node_modules/
    app/
      index.html
      server.js
    

    After printing out the __dirname path, I realized that the __dirname path was where my server was running (app/).

    So, the answer to your question is this:

    If your server.js file is in the same folder as the files you are trying to render, then

    app.use( express.static( path.join( application_root, 'site') ) );
    

    should actually be

    app.use(express.static(application_root));
    

    The only time you would want to use the original syntax that you had would be if you had a folder tree like so:

    app/
      index.html
    node_modules
    server.js
    

    where index.html is in the app/ directory, whereas server.js is in the root directory (i.e. the same level as the app/ directory).

    Side note: Intead of calling the path utility, you can use the syntax application_root + 'site' to join a path.

    Overall, your code could look like:

    // Module dependencies.
    var application_root = __dirname,
    express = require( 'express' ), //Web framework
    mongoose = require( 'mongoose' ); //MongoDB integration
    
    //Create server
    var app = express();
    
    // Configure server
    app.configure( function() {
    
        //Don't change anything here...
    
        //Where to serve static content
        app.use( express.static( application_root ) );
    
        //Nothing changes here either...
    });
    
    //Start server --- No changes made here
    var port = 5000;
    app.listen( port, function() {
        console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
    });
    
    0 讨论(0)
  • 2020-12-16 16:39

    Have you checked your folder structure? It seems to me like Express can't find your root directory, which should be a a folder named "site" right under your default directory. Here is how it should look like, according to the tutorial:

    node_modules/
      .bin/
      express/
      mongoose/
      path/
    site/
      css/
      img/
      js/
      index.html
    package.json
    

    For example on my machine, I started getting the same error as you when I renamed my "site" folder as something else. So I would suggest you check that you have the index.html page inside a "site" folder that sits on the same path as your server.js file.

    Hope that helps!

    0 讨论(0)
  • 2020-12-16 16:45

    If you are getting this error, it could be because you don't have a route defined for your get.

    For example:

    const express = require('express');
    
    const app = express();
    
    app.get('/people', function (req, res) {
        res.send('hello');
    })
    
    app.listen(3000);
    
    
    http://http://localhost:3000/people --> this works
    http://http://localhost:3000 --> this will output Cannot GET / message.
    
    0 讨论(0)
  • 2020-12-16 16:50

    I think you're missing your routes, you need to define at least one route for example '/' to index.

    e.g.

    app.get('/', function (req, res) {
      res.render('index', {});
    });
    
    0 讨论(0)
提交回复
热议问题