这个环回代码有什么问题?

时间:2017-06-25 11:11:02

标签: node.js loopbackjs

我正在尝试测试示例模型create,但它会显示Cannot GET /explorer/并浏览localhost:3001/explorer

var loopback = require('loopback');
var app = module.exports = loopback();

var Item = loopback.createModel(
  'Item',
  {
    description: 'string',
    completed: 'boolean'
  }
);

app.model(Item);
app.use('/api', loopback.rest());

app.start = function() {
  // start the web server
  return app.listen(function() {
    app.emit('started');
    var baseUrl = app.get('url').replace(/\/$/, '');
    console.log('Web server listening at: %s', baseUrl);
    if (app.get('loopback-component-explorer')) {
      var explorerPath = app.get('loopback-component-explorer').mountPath;
      console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
    }
  });
};

app.listen(3001);

1 个答案:

答案 0 :(得分:0)

这里说你应该把它放在app.listen之前(8080);所以基本上你的代码结构应该是这样的

var loopback = require('loopback');
var app = module.exports = loopback();

//you add the items here like 
/* 
POST /Items
GET /Items
PUT /Items
PUT /Items/{id}
HEAD /Items/{id}
GET /Items/{id}
DELETE /Items/{id}
GET /Items/{id}/exists
GET /Items/count
GET /Items/findOne
POST /Items/update
*/
var Item = loopback.createModel(
'Item',
{
description: 'string',
completed: 'boolean'
}
);

app.model(Item);
app.use('/api', loopback.rest());

    app.start = function() {
// start the web server
return app.listen(function() {
app.emit('started');
var baseUrl = app.get('url').replace(/\/$/, '');
console.log('Web server listening at: %s', baseUrl);
if (app.get('loopback-component-explorer')) {
var explorerPath = app.get('loopback-component-explorer').mountPath;
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
}
});
};
app.listen(8080);