postgresql - Invalid status code : 0 in node.js API -


i building simple api in node , express postgre db following tutorial.i still learning , new tech.i followed said , gave full permission postgre thinking issue db.but when check postman getting error invalid status code : 0

my 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  // allows client applications other domains use 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 , forward error handler  app.use(function(req, res, next) {      var err = new error('not found');      err.status = 404;      next(err);  });    // development error handler  // 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 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 * starships')      .then(function (data) {        res.status(200)          .json({            status: 'success',            data: data,            message: 'retrieved 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 , prosper!'        });  });        var db = require('./queries');    router.get('/api/starships', db.getallstarships);      module.exports = router;

querying db working fine pgadmin editor , returning table.

sample db

drop database if exists startrek; 

create database startrek;

\c startrek;

create table starships ( id serial primary key, name varchar, registry varchar, affiliation varchar, launched integer, class varchar, captain varchar );

insert starships (name, registry, affiliation, launched, class, captain) values ('uss enterprise', 'ncc-1701', 'united federation of planets starfleet', 2258, 'constitution', 'james t. kirk');

any appreciated. thanks.

got this..problem authentication.modified query.js with

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

and worked.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -