Unable to retrieve data from API using Express NodeJS and MongoDB, loading - javascript

I'm attempting to create a Rest API using Node.js, Express, and MongoDB. I am currently running on my local host :3000. When I try to restart and run the server I am using the route http://localhost:3000/drinks
I use Postman to send HTTP requests.
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en
While trying to send the route above, it does not retrieve any information. It just continues load.
This is my first time creating a REST API and I'm not sure why it isn't retrieving the data. Attached below is my code. Thanks in advance!
server.js
var express = require('express'),
drink = require('./routes/drinks');
var app = express();
app.configure(function () {
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
});
app.get('/drinks', drink.findAll);
app.get('/drinks/:id', drink.findById);
app.listen(3000);
console.log('Listening on port 3000...');
drinks.js
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('drinkdb', server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'drinkdb' database");
db.collection('drinks', {strict:true}, function(err, collection) {
if (err) {
console.log("The 'drinks' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
exports.findById = function(req, res) {
var id = req.params.id;
console.log('Retrieving drink: ' + id);
db.collection('drinks', function(err, collection) {
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
res.send(item);
});
});
};
exports.findAll = function(req, res) {
db.collection('drinks', function(err, collection) {
collection.find().toArray(function(err, drinks) {
res.send(drinks);
});
});
};
/*---------------------------------------------------------------------------------------------------------------*/
// Populate database with sample data -- Only used once: the first time the application is started.
// You'd typically not find this code in a real-life app, since the database would already exist.
var populateDB = function() {
var drinks = [
{
id: "1",
name: "Margarita",
ingredients: ["Tequila","Lime juice","Triple Sec","Lime","Salt","Ice"],
measurements: ["2 oz","1 oz","1 oz","1","optional","optional"],
directions: "Shake the other ingredients with ice, then carefully pour into the glass. Served: On the roc\
ks; poured over ice. Optional: Salt the rim of the glass by rubbing lime on it so it sticks."
},
{
id: "2",
name: "Strawberry Margarita",
ingredients: ["Tequila", "Lime juice","Triple Sec","Strawberries","Lime","Salt", "Ice"],
measurements: ["2 oz","1 oz", "1 oz", "3 1/2 cups", "1", "optional", "optional"],
directions: "Combine strawberries, ice, tequila, lime juice, and triple sec in a blender, and process unt\
il the mixture is smooth. Carefully pour into the glass. Served: On the rocks; poured over ice. Optional: Salt the ri\
m of the glass by rubbing lime on it so it sticks."
}];
db.collection('drinks', function(err, collection) {
collection.insert(drinks, {safe:true}, function(err, result) {});
});
};
Warnings are:
express deprecated app.configure: Check app.get('env') in an if statement server.js:6:5
connect deprecated multipart: use parser (multiparty, busboy, formidable) npm module instead node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:56:20
connect deprecated limit: Restrict request size at location of read node_modules/express/node_modules/connect/lib/middleware/multipart.js:86:15

I think Ashley is on the right track. But to make it more clear where the problem is happening try using this as a guide:
http://expressjs.com/en/guide/routing.html
app.get('/drinks', function (req, res) {
drink.findAll(req, res);
});
Then you can add logging in between this call and in your findAll function.

Your models (drinks.js) accept two parameters (req & res) but on your route you don't pass in any parameters.
Try the following:
app.get('/drinks', function(req, res) {
drink.findAll(req, res);
});
app.get('/drinks/:id', function(req, res){
drink.findById(req, res);
});
Alternatively, you could achieve the same with a callback based structure:
server.js
...
app.get('/drinks', function(req, res) {
drink.findAll(function(err, drinks){
res.send(drinks)
});
});
...
drinks.js
...
exports.findAll = function(callback) {
db.collection('drinks', function(err, collection) {
collection.find().toArray(function(err, drinks) {
callback(err, drinks)
});
});
};
(error handling required)
...

Related

How to improve node js code for production and split it properly across files?

I'm new to javascript and node.js.
Can someone answer the following questions.
1. How I split the PostgreSQL part properly in an other file.
2. How I the pest practice is to use the pg pools.
3. How I improve this code for production.
const express = require('express');
const app = express();
const pg = require('pg');
const pool = new pg.Pool({
user: 'admin',
password: 'test123!',
host: '127.0.0.1',
port: '5432',
database: 'test_db'
});
app.get('/api/recipes', function(req, res){
pool.connect(function(err, client, done) {
if(err){
console.log('Connection failed '+ err);
res.status(400).send(err);
}
client.query('SELECT * FROM recipes;', function(err, result) {
done();
if(err){
console.log('Error with query! ERROR code: ' + err.code);
res.status(400).send(err);
}
else{
res.status(200).send(result.rows)
}
});
});
});
app.get('/api/recipes/:id', function(req, res){
var id = req.params.id;
pool.connect(function(err, client, done) {
if(err){
console.log('Connection failed ' + err);
res.status(400).send(err);
}
else{
client.query('SELECT * FROM recipes WHERE recipes_id = $1;', [id], function(err, result) {
done();
if(err){
console.log('Error with query! ERROR code: ' + err.code);
res.status(400).send(err);
}
else{
res.status(200).send(result.rows)
}
});
}
});
});
app.listen(3000,function(){
console.log('Server listen on port 3000');
});
There are a lot of ways folks go to split the code you've described. I'll take it piece by piece.
First, pull any configurable variables out and setup one file that can get them from the environment (possibly with dev defaults in place, your choice on that). You can use a library like commander or convict, but honestly I prefer to just write a simple file that pulls them myself:
// ./config.js
module.exports = {
pool: {
user: process.env.DB_USER || 'admin',
password: process.env.DB_PW || 'test123!',
host: process.env.DB_HOST || '127.0.0.1',
port: process.env.DB_PORT || '5432',
database: process.env.DB_NAME || 'test_db'
}
};
As for your database calls, some folks like to use ORM-like stuff such as sequelize, but again I tend to start simple and add things as needed. In your case, you should think about what boilerplate stuff you can make common code around, and then wrap those into simple modules that only expose to the calling code stuff it really needs. For example, you will note that most of your routes are going to connect to the pool, test for an error, then run a query if it doesn't error out, and finally render either the error or query results, right? So that can all be wrapped into a fairly simple query function that handles the boilerplate internally and works with just a query expression and a callback, for example:
// ./db/index.js
const pg = require('pg');
const config = require('./config');
const pool = new pg.Pool(config.pool);
function query(sql, params, callback) {
// maybe check for valid inputs here or something, but at least normalize in case folks don't pass params
if(arguments.length < 3) {
callback = params;
params = null;
}
pool.connect((err, client, done) => {
// just exit here and let the calling code know there was a problem
if(err) return callback(err);
// I haven't tested this w/ the pg library recently, you might have to do two of these if it doesn't like null as a second argument
client.query(sql, params, (err, result) => {
if(err) return callback(err);
done();
// calling code probably doesn't care about anything but rows, but you can do other stuff here if you prefer
return callback(null, result.rows);
});
});
};
// You can also add additional functions if you want shorthand for doing things like query by ID or with params, or similar
module.exports = { query };
I also think that it can be helpful to store the SQL strings somewhere centrally, or on model objects, just to make the routing code note have to care about that. For a super simple example using your two routes, I might do something like this:
// ./db/queries.js
module.exports = {
RECIPES: {
LIST: 'SELECT * FROM recipes;',
FIND_BY_ID: 'SELECT * FROM recipes WHERE recipes_id = $1;'
}
};
Ok, so now your routing code can be quite simple, you can just get the db module and work the query, letting the routing worry just about what it's got to do with the request and response. Another option that folks like is to actually create a module for each model in your app (e.g. a Recipe) that wraps the above two files into a set of static functions so that your routes don't even know they're querying specifically. The calls in that case would be something like Recipe.list(cb) or Recipe.findById(id, cb). This is a style made popular by Ruby on Rails a few years ago, it has mixed acceptance in the Node community, but I'm mentioning it for completeness.
// ./routes/recipes.js
const router = require('express').Router();
const db = require('./db');
const queries = require('./db/queries');
router.get('/api/recipes', (req, res, next) => {
db.query(queries.RECIPES.LIST, (err, rows) => {
if(err) return next(err);
return res.send(rows); // status 200 is the default here
});
});
router.get('/api/recipes/:id', (req, res, next) => {
const id = req.params.id;
db.query(queries.RECIPES.FIND_BY_ID, [id], (err, rows) => {
if (err) return next(err);
return res.send(rows);
});
});
Finally, in your main Express setup file:
// ./app.js
const express = require('express');
const app = express();
const recipeRoutes = require('./routes/recipes') // note if you have an index.js file that gets imported just by calling for the folder, so that's a way to group features as well
app.use(recipeRoutes);
// I'm a big fan of error handling middleware. There's a more complex approach I did in [praeter][4] that gives you http-verb based errors that you can then catch and send the appropriate status, but that's again more complex than you might need here.
app.use((err, req, res, next) => {
// this can be as simple or as complex as you like.
// note it's a best practice to send only "clean" messages to the client, so you don't give away that you're using a Postgres db or other stuff that makes hacking easier.
console.error(err);
res.status(500).send('Oops! Something went wrong!!');
});
Obviously, there's a lot of ways to skin this cat, so I'd recommend mostly just looking for where you're repeating yourself, and then refactor to repeat less. Also, if you're interested in making more production-ready apps in general, the 12 factor app is a must-read.
To answer number 1,
dbPool.js
const pg = require('pg');
export.pool = new pg.Pool({
user: 'admin',
password: 'test123!',
host: '127.0.0.1',
port: '5432',
database: 'test_db'
});
app.js
const express = require('express');
const app = express();
const pool = require('./dbPool');
....
You should create config file and require that file in app.js
--config
----config.js
--app.js
var config = {
production: {
pool: {
user: 'admin',
password: 'test123!',
host: '127.0.0.1',
port: '5432',
database: 'test_db'
}
},
development: {
pool: {
user: 'admin',
password: 'test123!',
host: '127.0.0.1',
port: '5432',
database: 'test_db'
}
}
}
exports.get = function get(env) {
return config[env] || config.development;
}

Node.js Express POST call not working although GET does

I am trying to write a REST backend using Node.js, express and MongoDB but am having some issues creating PUT calls for some reason. The issue is with app.post('/contacts/add', contacts.addContacts) as it works fine if I change it to GET but when I change it to either PUT or POST I get the error Cannot GET /contacts/add Any ideas?
I am using express 4.15.3, mongodb 3.4.5 and npm 4.2.0
server.js:
var express = require('express'),
contacts = require('./routes/contacts');
bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.get('/contacts/chalkboard/:id', contacts.getChalkboardContacts);
app.get('/contacts/get/:uid', contacts.getContacts);
app.post('/contacts/add', contacts.addContacts);
app.listen(3000);
console.log('Listening on port 3000...');
contacts.js
var mongo = require('mongodb');
mongo.BSONPure = require('bson').BSONPure;
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('db', server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to database");
db.collection('contacts', {strict:true}, function(err, collection) {
if (err) {
console.log("The 'contacts' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
exports.getChalkboardContacts = function(req, res) {
var uid = req.params.uid.toString();
var date = new Date();
var timeInMs = date.getMilliseconds();
console.log(uid);
db.collection('contacts', function(err, collection) {
console.log('collection: ' + collection);
collection.find({uid: uid, lastMessage: {$gte: timeInMs}}).toArray(function(err, items) {
res.send(items);
});
});
};
exports.getContacts = function(req, res) {
var uid = req.params.uid.toString();
console.log(uid);
db.collection('contacts', function(err, collection) {
console.log('collection: ' + collection);
collection.find({uid: uid}).toArray(function(err, items) {
res.send(items);
});
});
};
exports.addContacts = function(req, res) {
console.log('working');
db.collection('contacts', function(err, collection) {
var id = "592159bc3e48764418170399";
var contact = {uid: "592159bc3e48764418173333",
keyUid: "592159bc3e48764418171444",
name: "Billy Jean",
phoneNumber: "+491721894733",
battery: "47%", longitude: "0",
latitude: "0",
city: "city",
country: "country",
place: "place",
address: "address",
lastMessage: "lastMessage",
lastUpdated: "lastUpdated"};
collection.update({'uid':id}, {$push:{'contacts':contact}}, function(err, result) {
if (err) {
console.log('Error updating contact: ' + err);
res.send({'error':'An error has occurred'});
} else {
console.log('' + result + ' document(s) updated');
res.send(result);
}
});
});
};
I didn't spot anything immediately wrong with the code.
You might be falling into a very common pitfall when using body-parser for JSON. Your request must specify Content-Type: application/json in the request for the parser to parse it. Otherwise, it won't work.
If that doesn't do the trick, if you can share any error messages or response codes you get, it may illuminate another issue.

MongoDB get data to display in HTML

I'm trying to display mongodb data in my html page. I've already managed to insert data in db but for some reason my "get" function does not work.
I'm using node.js with express framework and Angular for front-end and routing.
This is my "get" function to retreive data from MongoDB:
var mongo = require('mongodb');
var assert = require('assert');
var url = 'mongodb://localhost:27017/loodgieters';
router.get('/get-data', function(req, res, next) {
var resultArray = [];
mongo.connect(url, function(err, db){
assert.equal(null, err);
var cursor = db.collection('user-data').find();
cursor.forEach(function(doc, err){
assert.equal(null, err);
resultArray.push(doc);
}, function(){
db.close();
res.render('index', {items: resultArray});
});
});
});
And my "post" which works
router.post('/insert', function(req, res, next) {
var item = {
name: req.body.name,
adress: req.body.adress,
postal: req.body.postal,
city: req.body.city,
email: req.body.email,
phone: req.body.phone,
quotation: req.body.quotation,
message: req.body.message
};
mongo.connect(url, function(err, db) {
assert.equal(null, err);
db.collection('user-data').insertOne(item, function(err, result){
assert.equal(null, err);
console.log('Item inserted');
db.close();
});
});
res.redirect('/contact');
});
i am not sure if this is the correct way to open and close mongo connection each time you are trying to query .
if you want to go for another approach then use mongoose
and follow something like this
https://pastebin.com/g7aatzzj
I think that you have a mistake in your .find().forEach function callbacks. The error handling seems to be in the endCallback not the iteratorCallback.
According to the official doc, the correct way should be :
var mongo = require('mongodb');
var assert = require('assert');
var url = 'mongodb://localhost:27017/loodgieters';
router.get('/get-data', function(req, res, next) {
var resultArray = [];
mongo.connect(url, function(err, db){
assert.equal(null, err);
var cursor = db.collection('user-data').find({});
cursor.forEach(function(doc){
assert.notEqual(null, doc);
resultArray.push(doc);
}, function(err, doc){
assert.equal(null, err);
db.close();
res.render('index', {items: resultArray});
});
});
});
This can also be found in their unit tests
var cursor = collection.find({})
.map(function(x) { return {a:1}; })
.batchSize(5)
.limit(10);
cursor.forEach(function(doc) {
test.equal(1, doc.a);
}, function(err, doc) {
test.equal(null, err);
db.close();
test.done();
});
I think that you must have a error that is not passed to the first callback and not handled in the second one. So you do not see the error.
Try to insert an empty object to the find() function as following:
var cursor = db.collection('user-data').find({});
I have just run your code and modified it a bit for my purposes.
Please find the following snippet
//Instantiate MongoClient
var mongo = require('mongodb').MongoClient;
//Assert library (Perhaps overkill if you are writing production-level code)
var assert = require('assert');
//Express engine
var express = require('express');
//URL for my mongo instance
//Connecting to the blog database
var url = 'mongodb://localhost:27017/blog';
//Instantiate express
var router = express();
//Get operation
router.get('/get', function(req, res, next) {
var resultArray = [];
mongo.connect(url, function(err, db){
assert.equal(null, err);
var cursor = db.collection('posts').find();
cursor.forEach(function(doc, err){
assert.equal(null, err);
resultArray.push(doc);
}, function(){
db.close();
//I have no index file to render, so I print the result to console
//Also send back the JSON string bare through the channel
console.log(resultArray);
res.send(resultArray);
});
});
});
//Start listeninig
//Hardcoded port 1000
var server = router.listen(1000, function() {
var host = server.address().address;
var port = server.address().port;
console.log("Content Provider Service listening at http://%s:%s", host, port);
});
Therefore to get this working for you:
Change the url to 'mongodb://localhost:27017/loodgieters';
Change router to '/get-data'
I hope this helps!
Also consider using splitting the implementation of the get operation to another module to help for the Separation of Responsibilities to make your code more robust.

Creating Rest API using Node.js, Express, and MongoDB .post() error

I'm attempting to create a Rest API using Node.js, Express, and MongoDB. I am currently running on my local host :3000. When I try to restart and run the server I am getting this error:
Error: .post() requires callback functions but got a [object Undefined]
Attached below is my code.
I am new to this, not sure what that error is or how to fix. Thanks in advance!
server.js
var express = require('express'),
drink = require('./routes/drinks');
var app = express();
app.configure(function () {
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
});
app.get('/drinks', drink.findAll);
app.get('/drinks/:id', drink.findById);
app.post('/drinks', drink.addWine);
app.put('/drinks/:id', drink.updateWine);
app.delete('/drinks/:id', drink.deleteWine);
app.listen(3000);
console.log('Listening on port 3000...');
drinks.js
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('drinkdb', server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'drinkdb' database");
db.collection('drinks', {strict:true}, function(err, collection) {
if (err) {
console.log("The 'drinks' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
exports.findById = function(req, res) {
var id = req.params.id;
console.log('Retrieving drink: ' + id);
db.collection('drinks', function(err, collection) {
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
res.send(item);
});
});
};
exports.findAll = function(req, res) {
db.collection('drinks', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
});
});
};
exports.addDrink = function(req, res) {
var drink = req.body;
console.log('Adding drink: ' + JSON.stringify(drink));
db.collection('drinks', function(err, collection) {
collection.insert(drink, {safe:true}, function(err, result) {
if (err) {
res.send({'error':'An error has occurred'});
} else {
console.log('Success: ' + JSON.stringify(result[0]));
res.send(result[0]);
}
});
});
};
exports.updateDrink = function(req, res) {
var id = req.params.id;
var drink = req.body;
console.log('Updating drink: ' + id);
console.log(JSON.stringify(drink));
db.collection('drinks', function(err, collection) {
collection.update({'_id':new BSON.ObjectID(id)}, drink, {safe:true}, function(err, result) {
if (err) {
console.log('Error updating drink: ' + err);
res.send({'error':'An error has occurred'});
} else {
console.log('' + result + ' document(s) updated');
res.send(drink);
}
});
});
};
exports.deleteDrink = function(req, res) {
var id = req.params.id;
console.log('Deleting drink: ' + id);
db.collection('drinks', function(err, collection) {
collection.remove({'_id':new BSON.ObjectID(id)}, {safe:true}, function(err, result) {
if (err) {
res.send({'error':'An error has occurred - ' + err});
} else {
console.log('' + result + ' document(s) deleted');
res.send(req.body);
}
});
});
};
/*--------------------------------------------------------------------------------------------------------------------*/
// Populate database with sample data -- Only used once: the first time the application is started.
// You'd typically not find this code in a real-life app, since the database would already exist.
var populateDB = function() {
var drinks = [{
id: "1",
name: "Margarita",
//ingredients: ["Tequila","Lime juice","Triple Sec","Lime","Salt","Ice"],
//measurements: ["2 oz","1 oz","1 oz","1","optional","optional"],
directions: "Shake the other ingredients with ice, then carefully pour into the glass. Served: On the rocks; poured over ice. Optional: Salt the rim of the glass by rubbing lime on it so it sticks."
},{
id: "2",
name: "Strawberry Margarita",
//ingredients: ["Tequila", "Lime juice","Triple Sec","Strawberries","Lime","Salt", "Ice"],
//measurements: ["2 oz","1 oz", "1 oz", "3 1/2 cups", "1", "optional", "optional"],
directions: "Combine strawberries, ice, tequila, lime juice, and triple sec in a blender, and process until the mixture is smooth. Carefully pour into the glass. Served: On the rocks; poured over ice. Optional: Salt the rim of the glass by rubbing lime on it so it sticks."
}];
db.collection('drinks', function(err, collection) {
collection.insert(drinks, {safe:true}, function(err, result) {});
});
};
As I see in the drinks.js you are exporting addDrink function but in the server.js you are trying to use undefined addWine function.
Try to rename Wine to Drink in server.js routes handlers.

ExpressJS POST not submitting data, using mongoose

I am building my first express.js application and I have run into my first hurdle.
I have a very simple set up.
routes in app.js:
app.get('/', routes.index);
app.get('/users', user.list);
app.get('/products', product.all);
app.post('/products', product.create);
route(controller) in routes/product.js
var Product = require('../models/product.js');
exports.create = function(req, res) {
new Product({ name: req.query.name, description: req.query.description }).save();
};
exports.all = function(req, res) {
Product.find(function(err, threads) {
if (err) {
res.send('There was an error: ' + err)
}
else {
res.send(threads)
}
});
}
Product model in models/product.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var productSchema = new Schema({
name: String,
description: String
});
module.exports = mongoose.model('Product', productSchema);
I have tried sending post requests with both Postman (chrome extension) as well as Curl. I have noticed that both seem to hang after sending the request, as if waiting for a response, i'm no http expert but I assumed a post would not have a response? But perhaps it responds with whether it was successful or not?
my sample requests:
http://0.0.0.0:3000/products?name=Cool, http://0.0.0.0:3000/products?name=Cool%Product&description=Allo%there%Soldier!
After sending the post and then sending a get request to http://0.0.0.0:3000/products I get an array of objects like so:
{
"_id": "52e8fe40b2b3976033ae1095",
"__v": 0
},
{
"_id": "52e8fe81b2b3976033ae1096",
"__v": 0
},
These are equal to the number of post requests I have sent, indicating to me that the server is receiving the post and creating the document/file, but not actually passing the parameters in.
Some help here would be excellent!
EDIT: It seems the code above is fine, I think I may have forgotten to restart my node server after having made some changes (Doh!), the restart fixed the issue
there is something like an http-request lifecycle, and of course an post has a response.
probably something like a 200 if your insert worked and a 404 if not!
you need to send a response in your create method:
exports.create = function(req, res) {
new Product({ name: req.query.name, description: req.query.description }).save();
res.send('saved');
};
Your post needs a response. You could do something like
var newProduct = new Product({ name: req.query.name, description: req.query.description });
newProduct.save(function(err, entry) {
if(err) {
console.log(err);
res.send(500, { error: err.toString()});
}
else {
console.log('New product has been posted.');
res.send(JSON.stringify(entry));
}
});

Categories

Resources