“Cannot GET /” with Connect on Node.js

后端 未结 10 1227
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 00:40

I\'m trying to start serving some static web pages using connect like this:

var connect = require(\"connect\");
var nowjs = require(\"now\");
va         


        
相关标签:
10条回答
  • 2020-12-01 01:07

    You may be here because you're reading the Apress PRO AngularJS book...

    As is described in a comment to this question by KnarfaLingus:

    [START QUOTE]

    The connect module has been reorganized. do:

    npm install connect 
    

    and also

    npm install serve-static
    

    Afterward your server.js can be written as:

    var connect = require('connect');
    var serveStatic = require('serve-static'); 
    var app = connect(); 
    
    app.use(serveStatic('../angularjs')); 
    
    app.listen(5000);
    

    [END QUOTE]

    Although I do it, as the book suggests, in a more concise way like this:

    var connect = require('connect');
    var serveStatic = require('serve-static');
    
    connect().use(
        serveStatic("../angularjs")
    ).listen(5000);
    
    0 讨论(0)
  • 2020-12-01 01:09

    The easiest way to serve static files is to use "harp". It can be found here. You can serve up your files from the location you want via node is:

    var harp = require("harp")
    harp.server(projectPath [,args] [,callback])
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-01 01:12

    This code should work:

    var connect = require("connect");
    
    var app = connect.createServer().use(connect.static(__dirname + '/public'));
    
    app.listen(8180);
    

    Also in connect 2.0 .createServer() method deprecated. Use connect() instead.

    var connect = require("connect");
    
    var app = connect().use(connect.static(__dirname + '/public'));
    
    app.listen(8180);
    
    0 讨论(0)
  • 2020-12-01 01:15

    You'll see the message Cannot GET / if you don't specify which page it is that you're trying to get, in other words if your URL is something like http://localhost:8180. Make sure you enter a page name, e.g. http://localhost:8180/index.html.

    0 讨论(0)
  • 2020-12-01 01:20

    You may also want to try st, a node module for serving static files. Setup is trivial.

    npm install connect
    
    npm install st
    

    And here's how my server-dev.js file looks like:

    var connect = require('connect');
    var http = require('http');
    var st = require('st');
    
    var app = connect()
        .use(st('app/dev'));
    
    http.createServer(app).listen(8000);
    

    or (with cache disabled):

    var connect = require('connect');
    var http = require('http');
    var st = require('st');
    
    var app = connect();
    
    var mount = st({
      path: 'app/dev',
      cache: false
    });
    
    http.createServer(function (req, res) {
      if (mount(req, res)) return;
    }).listen(8000);
    
    app.use(mount);
    
    0 讨论(0)
  • 2020-12-01 01:21
    var connect = require('connect');
    var serveStatic = require('serve-static');
    var app = connect(); 
    app.use(serveStatic('../angularjs'),  {default: 'angular.min.js'}); app.listen(3000); 
    
    0 讨论(0)
提交回复
热议问题