NodeJS服务器错误

时间:2015-09-03 01:24:21

标签: javascript angularjs node.js

我学习Angular的第三天。我已经达到了学习ajax调用的程度,但我已经停留在教程要求我运行server.js的地方。我安装了nodejsexpress,但是当我尝试运行node server.js

编辑:

我尝试按照其网站上的示例将代码从express 3.0更改为express 4.0。我的新server.js文件如下所示:

server.js

var express = require('express'),
    app = express();

/* EXPRESS 3.0
app.configure(function () {
    app.use(express.static(__dirname, '/'));
});
*/

// EXPRESS 4.0
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
   // configure stuff here
    app.use(express.static(__dirname, '/'));
}

/*EXPRESS 3.0
app.get('/customers/:id', function(req, res) {
    var customerId = parseInt(req.params.id);
    var data = {};
    for (var i = 0, len = cutomers.length; i < len; i++) {
        if (customers[i].id === customerId) {
            data = customer[i];
            break;
        }
    }
    res.json(data)
});
*/

//EXPRESS 4.0
app.route('/customers/:id')
    .get(function(req, res) {
    var customerId = parseInt(req.params.id);
    var data = {};
    for (var i = 0, len = cutomers.length; i < len; i++) {
        if (customers[i].id === customerId) {
            data = customer[i];
            break;
        }
    }
    res.json(data)
    })

/* EXPRESS 3.0
app.get('/customers', function(req, res) {
    res.json(customers);
});
*/

//EXPRESS 4.0
app.route('/customers')
    .get (function(req, res) {
    res.json(customers);
})


app.listen(3000);

console.log('Express listening on port 3000');


var customers = [
          { 
            id: 1,
            joined: '2005-09-07', 
            name: 'Mayweather', 
            city: 'Brooklyn', 
            orderTotal: '43.1299',
            orders: [
              {
                id: 1,
                product: 'Pencils',
                total: 9.9956
              }
            ]

          }, 
          {
            id: 2,
            joined: '2005-09-07', 
            name: 'Jason', 
            city: 'Cleveland', 
            orderTotal: '89.8933',
            orders: [
              {
                id: 1,
                product: 'iPad',
                total: 20.9956
              }
            ]  
          }, 
          {
            id: 3,
            joined: '1999-08-27', 
            name: 'Jade', 
            city: 'Wroclaw', 
            orderTotal: '77.0092',
            orders: [
              {
                id: 1,
                product: 'Pillows',
                total: 12.2311
              }
            ]
          }, 
          {
            id: 4,
            joined: '2015-09-01', 
            name: 'David', 
            city: 'Accra', 
            orderTotal: '13.8465',
            orders: [
              {
                id: 1,
                product: 'Serek',
                total: 11.4782
              }
            ]
          }, 
          {
            id: 5,
            joined: '2001-01-18', 
            name: 'Doyet',
            city: 'Paris',
            orderTotal: '23.9930',
            orders: [
              {
                id: 1,
                product: 'Serek',
                total: 11.4782
              }
            ]
          }];

终端看起来像这样:

node server.js
/Users/siaw/Desktop/angularjshelloworld/node_modules/express/node_modules/serve-static/index.js:47
  var opts = Object.create(options || null)
                    ^
TypeError: Object prototype may only be an Object or null: /
    at Function.create (native)
    at Function.serveStatic (/Users/siaw/Desktop/angularjshelloworld/node_modules/express/node_modules/serve-static/index.js:47:21)
    at Object.<anonymous> (/Users/siaw/Desktop/angularjshelloworld/server.js:15:27)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

我能解决这个问题吗?

4 个答案:

答案 0 :(得分:1)

Application.configure()被记录为3.x中的遗留和4.x

中的removed

答案 1 :(得分:1)

如果您使用的是Express 4.0,则会删除配置。见http://expressjs.com/guide/migrating-4.html#other-changes简单的错误。

答案 2 :(得分:0)

您是否尝试过安装快递?在您的控制台上执行npm install -g express(如果您没有足够的权限,可能需要sudo npm install -g express),并查看错误是否消失。

答案 3 :(得分:0)

只需要改为

app.use(express.static(__dirname + '/'));代替app.use(express.static(__dirname, '/'));

**感谢@stride on IRC。