【问题标题】:Invalid status code : 0 in node.js API无效状态码:node.js API 中的 0
【发布时间】:2017-09-02 20:02:19
【问题描述】:

我正在节点中构建一个简单的 api,并按照教程将 postgre 表达为 Db。我仍在学习这项技术并且是新手。我按照所说的一切进行操作,并完全允许 ​​postgre 考虑我的 db 问题。但是当我与邮递员检查时,我收到此错误无效状态代码:0

我的 server.js

var express = require('express');
var logger = require('morgan');
var bodyParser = require('body-parser');

var api = require('./api/index');

var app = express();


///////////////////////
// Server Middleware
///////////////////////

app.use(logger(app.get("env") === "production" ? "combined" : "dev"));

// parse application/json
app.use(bodyParser.json());

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// CORS
// This allows client applications from other domains use the API Server
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});


//////////////////
// API Queries
//////////////////

app.use('/', api);


//////////////////
// Server Setup
//////////////////

app.set("env", process.env.NODE_ENV || "development");
app.set("host", process.env.HOST || "0.0.0.0");
app.set("port", process.env.PORT || 8080);

app.listen(app.get("port"), function () {
    console.log('\n' + '**********************************');
    console.log('REST API listening on port ' + app.get("port"));
    console.log('**********************************' + '\n');
});


////////////////////
// Error Handlers
////////////////////

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status( err.code || 500 )
        .json({
            status: 'error',
            message: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500)
    .json({
        status: 'error',
        message: err.message
    });
});


module.exports = app;

queries.js

var promise = require('bluebird');

var options ={
    promiseLib : promise
};

var pgp = require('pg-promise')(options);
var connectionString = 'postgres://localhost:5432/star';
console.log(connectionString);
var db = pgp(connectionString);


function getAllStarships(req, res, next) {
  db.any('SELECT * FROM starships')
    .then(function (data) {
      res.status(200)
        .json({
          status: 'success',
          data: data,
          message: 'Retrieved all starships'
        });
    })
    .catch(function (err) {
      return next(err);
    });
}


module.exports ={getAllStarships: getAllStarships,};

index.js

var express = require('express');
var router = express.Router();


router.get('/', function(req, res, next) {
    res.status(200)
      .json({
        status: 'success',
        message: 'Live long and prosper!'
      });
});



var db = require('./queries');

router.get('/api/starships', db.getAllStarships);


module.exports = router;

在 pgadmin 编辑器中查询 Db 工作正常并返回表。

示例数据库

DROP DATABASE IF EXISTS startrek;

创建数据库 startrek;

\c startrek;

CREATE TABLE 星舰 ( ID 序列主键, 名称 VARCHAR, 注册表 VARCHAR, 隶属关系 VARCHAR, 推出INTEGER, VARCHAR 类, VARCHAR船长 );

插入星舰(名称、登记处、隶属关系、发射、等级、船长) VALUES('USS Enterprise'、'NCC-1701'、'United Federation of Planets Starfleet'、2258、'Constitution'、'James T. Kirk');

感谢任何帮助。 谢谢。

【问题讨论】:

    标签: node.js postgresql express postgresql-9.4 pg-promise


    【解决方案1】:

    得到这个..问题出在身份验证上。用

    修改了我的 query.js

    var db = pgp({
        host: 'localhost',
        port: 5432,
        database: 'star',
        user: 'postgres',
        password: 'pes'
    });

    它奏效了。

    【讨论】:

    • 从您的配置中,您的连接字符串应该是:var connectionString = 'postgres://postgres:pes@localhost:5432/star';。请参阅Connection String Syntax
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2014-06-22
    • 1970-01-01
    • 2011-08-18
    相关资源
    最近更新 更多