Reputation: 382
I am building a simple api in node and express with postgre as Db following a tutorial.I am still learning and new to this tech.I followed everything as said and gave full permission to postgre thinking about issue with my db.But when i check it with postman i am getting this 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
// 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;
Querying Db is working fine from the pgadmin editor and returning the 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 INTO starships (name, registry, affiliation, launched, class, captain) VALUES ('USS Enterprise', 'NCC-1701', 'United Federation of Planets Starfleet', 2258, 'Constitution', 'James T. Kirk');
Any Help Appreciated. Thanks.
Upvotes: 0
Views: 973
Reputation: 382
Got this..Problem was with Authentication.Modified my query.js with
var db = pgp({
host: 'localhost',
port: 5432,
database: 'star',
user: 'postgres',
password: 'pes'
});
And it worked.
Upvotes: 0