快速配置不起作用

时间:2015-10-04 13:55:22

标签: express

我用

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

然后我发现它是旧版本。然后我用了这个,

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

但它仍然给我错误。我的整个代码如下。

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

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

app.get('/people/:id', function(req, res){`

var customerId = parseInt(req.params.id);
var data = {};
for(var i = 0; i < people.length;  i++){

    if(people[i].id === customerId){
        data = people[i];
        break;
    }
}

res.json(data);
});

app.get('/people', function(req, res){

res.json(people);

});

app.lisen(8080);

console.log('Listening on express port 8080');

var people = [
        {id: 1, name: 'John', city: 'Lisbon', gender: 'male', total: '515.561', 
         orders: [
                    {id: 1, product: 'Shoes', total: '100'}
                 ]
        }, 
        {id: 2, name:'Abbie', city:'Orlando', gender:'female', total:'3445.34', 
         orders: [
                    {id: 2, product: 'Shoes', total: '200.561'}
                 ]
        }, 
        {id: 3, name:'Will', city:'Houston', gender:'female', total:'98754.00', 
         orders: [
                    {id: 1, product: 'Shoes', total: '300.561'}, 
                    {id: 3, product: 'Shoes', total: '330.561'}
                 ]
        }, 
        {id: 4, name:'Jim', city:'Paris', gender:'male', total:'15.26', 
         orders: [
                    {id: 4, product: 'Shoes', total: '400.561'}
                 ]
        }, 
        {id: 5, name:'Bryan', city:'Lisbon', gender:'male', total:'515.561', 
         orders: [
                    {id: 5, product: 'Shoes', total: '500.561'}
                 ]
        },  
        {id: 6, name:'Agulera', city:'Orlando', gender:'female', total:'3445.34', 
         orders: [
                    {id: 6, product: 'Shoes', total: '600.561'}
                 ]
        },  
        {id: 7, name:'Christeen', city:'Houston', gender:'female', total:'98754.00', 
         orders: [
                    {id: 7, product: 'Shoes', total: '700.561'}
                 ]
        },  
        {id: 8, name:'Matt', city:'Paris', gender:'male',total:'15.26', 
         orders: [
                    {id: 8, product: 'Shoes', total: '800.561'}
                 ]
        }
];

我很陌生,我正在观看视频。所以我想正确配置并运行它。请帮帮我。

1 个答案:

答案 0 :(得分:0)

拼写错误

app.lisen(8080);

---&GT;

app.listen(8080);

...第二

全局变量是不好的策略,将var people作为app的局部变量:

http://expressjs.com/api.html#app.locals

app.locals.people = "put the people data here";

最后,您应该将响应和相关的http错误代码放在诊断错误的位置:

例如......

var yes = "yes";

if ( yes !== data.answer) {
console.log(err);
 res.status(401).send(err + "error, you do not have authorized access to this data"); }
else {
console.log("success! You are authorized!")
res.status(200).send(data);
};

发送回json对象数据&#34; ok&#34;状态代码,如果符合条件;

状态代码: http://www.w3.org/Protocols/HTTP/HTRESP.html