简体   繁体   中英

NodeJS server error

My third day on learning Angular. I reached a point of learning about making ajax calls but I'm stuck at a point where a tutorial asked me to run server.js . I have installed nodejs and express but when I try to run node server.js

EDIT:

I tried to changed the code, from express 3.0 to express 4.0 following the examples on their website. My new server.js file looks like this:

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
              }
            ]
          }];

And terminal looks like this:

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

Any way I can fix this?

Application.configure()在3.x中记录为旧版,在4.x中已删除

if you are using Express 4.0 then the configure is removed. See http://expressjs.com/guide/migrating-4.html#other-changes its Simple mistake.

Have you tried installing express? Do npm install -g express on your console (you may need to sudo npm install -g express if you don't have sufficient privileges), and see if the error goes away.

Just needed to change to

app.use(express.static(__dirname + '/')); instead of app.use(express.static(__dirname, '/'));

** Thanks to @stride on IRC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM