rest full service in express web application - node.js

I have to implement a express web application to use rest services with mongodb.
my DBConfig.js file is like below which include my db schema,
var Mongoose = require("mongoose");
const Schema = Mongoose.Schema;
const UserSchema = new Schema({
name : {
type: String,
require: true
},
address : {
type: String,
require: false
},
password: {
type: String,
require: true
}
});
Mongoose.model('User', UserSchema);
Mongoose.connect("mongodb://localhost:27017/demo", function (err) {
if (err) {
console.log(err);
process.exit(-1);
}
console.log("Connected");
});
module.exports = Mongoose;
all the relevant methods are implement in my Controller.js file
var Mongoose = require("../DBSchema/DBConfig");
var UserSchema = Mongoose.model('User');
var Controller = function() {
this.insertUser = function (data) {
return new Promise(function(resolve, reject) {
var User = new UserSchema({
name: data.name,
address: data.address,
password: data.password
});
User.save().then(function () {
resolve({status: 200, message: "User inserted successfully"})
}).catch(function (err) {
reject({status: 500, message: "Error:- " + err})
});
});
}
this.getAll = function () {
return new Promise(function (resolve, reject) {
UserSchema.find().exec().then(function (data) {
resolve({status: 200, Userdata: data})
}).catch(function (err) {
resolve({status: 500, message : "No data available"})
})
})
}
this.getuser = function (id) {
return new Promise(function (resolve, reject) {
UserSchema.find({_id: id}).exec().then(function (data) {
resolve({status: 200, userSearched: data});
}).catch(function (err) {
reject({status: 404, message: "User NOT FOUND"});
})
})
}
this.updateUser = function (id, data) {
return new Promise(function (resolve, reject) {
UserSchema.update({_id: id}, data).then(function (data) {
resolve({status: 200, message: "User updated successfully"})
}).catch(function (err) {
reject({status: 500, message: "Error:- " + err})
})
})
}
this.remove = function (id) {
return new Promise(function (resolve, reject) {
UserSchema.remove({_id: id}).then(function () {
resolve({status: 200, message: "Successfully deleted"})
}).catch(function (err) {
resolve({status: 500, message: "Error : "+err})
})
})
}
}
module.exports = new Controller();
now I have to call my rest services,I want to know how can I use these methods which are in my controller to access data.
I'm using postman to view my data.
the output should be
{
"data": [
{
"_id": "5cf724a58c9b061ba062a28c",
"name": "name1",
"address": "adress1",
"password": "password1",
"__v": 0
}
]
}

All you need is you have to define the routes in your URL to access relevant method, example I create a file called User.Routes.js under a folder called UserController which you can add your Controller.js file also to arrange in an order to get a clear idea. and also assume you add your DBConfig.js file under folder called DBSchema, which made you to do good code practise.
var Express = require('express');
var router = Express.Router();
var Controller = require('./Controller')
router.get('/',function (req,res) {
Controller.getAll().then(function (data) {
res.status(data.status).send({data:data.Userdata})
});
});
router.get('/:id',function (req,res) {
Controller.getuser(req.params.id).then(function (data) {
res.status(data.status).send({data:data.userSearched});
});
});
router.post('/',function(req,res){
Controller.insertUser(req.body).then(function (data) {
res.status(data.status).send({message: data.message});
}).catch(function (err) {
res.status(err.status).send({message: err.message});
});
});
router.put('/:id',function (req,res) {
Controller.updateUser(req.params.id,req.body).then(function (data) {
res.status(data.status).send({message: data.message});
});
});
router.delete('/:id', function (req,res) {
Controller.remove(req.params.id).then(function (data) {
res.status(data.status).send({message: data.message});
});
});
module.exports = router;
and now you need to use methods in your User.Routes.js via postman. for that you need to give url name to be accessed. example, I create Routes.js file
const Express = require("express");
var Routes = Express.Router();
var UserRoute = require('./UserController/User.Route');
Routes.use('/user/', UserRoute);
module.exports = Routes;
like this if you want to view data from your schema you have to add the url like -"http://localhost:8083/user"
and your app.js file want to require the created Routes file for that you can do like,
const Express = require("express");
const app = Express();
const BodyParser = require("body-parser");
const Routes = require("./Routes");
app.use(BodyParser.json());
app.use("/", Routes);
app.listen(8083, "localhost", function(err) {
if (err) {
console.log(err);
process.exit(-1);
}
console.log("Server listening on port 8083");
});
Now using postman you can add user,update and delete particular user by giving user id in your URL like - "http://localhost:8083/user/5cf724a58c9b061ba062a28c"

Related

Mongoose one-to-many not working (Nodejs)

I have created 2 Users(Admin and Users) and also i have created many ToDos for a User but here my Todo array is empty in my User Schema. Unable to understand why todo task are not assigned to the User Schema.
UserSchema
var userSchema = new Schema({
name: {
type: String,
required: true,
maxlength: 30,
trim: true
},
role: {
type: Number,
default: 0
},
todos: [{
type: Schema.Types.ObjectId,
ref:"Todo"
}]
});
module.exports = mongoose.model("User", userSchema)
Todo Schema
let Todo = new Schema({
todo_heading: {
type: String
},
todo_desc: {
type: String
},
todo_priority: {
type: String
},
todo_completed: {
type: Boolean
},
user: {
type: Schema.Types.ObjectId,
ref:"User"
}
})
module.exports = mongoose.model('Todo', Todo);
here are my routes
User Route
router.get("/user/:userId/todos", isSignedIn, isAuthenticated, getToDos)
Todo Route
router.get("/", getTodos)
router.get("/:id", getUsertodos);
router.post("/user/:userId/add", addUsertodos);
User Controllers
exports.getToDos = (req, res) => {
User.find({ _id: req.params._id })
.populate("todos")
.exec((err, toDo) => {
if (err) {
res.json(err)
}
res.json(toDo)
})
}
ToDo Controllers
exports.addUsertodos = (req, res) => {
let todo = new Todo(req.body)
todo.save((err, todo) => {
if (err) {
return res.status(400).json({
error: "not saved"
})
}
else {
return res.json(todo)
}
})
}
it should work as expected if you add the objectId of newly created todo to the todos property when you create a user.
//routers/todo.js
var express = require('express');
var router = express.Router();
const Todo = require('../models/Todo');
const User = require('../models/User');
/* GET home page. */
router.get('/', async function (req, res) {
let todos = await Todo.find();
res.json({
todos
});
});
router.post('/todos', async function (req, res) {
//add todos
let {
todo_desc,
todo_heading,
todo_priority,
todo_completed
} = req.body;
try {
//NOTE: for simplicity assigning first user but you can grab it from the params
let user = await User.findOne();
let todo = await Todo.create({
todo_desc,
todo_completed,
todo_priority,
todo_heading,
user: user._id
})
res.json({
message: 'todo created successfully',
todo
});
} catch (err) {
return res.status(500).json({
message: 'Unable to create a todo',
err: JSON.stringify(err)
})
}
});
module.exports = router;
Here is the user route where post route get the string id of created ID and converts it to ObjectId(), assign it to the todos.
var express = require('express');
var router = express.Router();
let _ = require('lodash');
var mongoose = require('mongoose');
const User = require('../models/User');
/* GET users listing. */
router.post("/", async function (req, res) {
let {
name,
todos
} = req.body;
try {
let user = new User();
user.name = name;
let objectIds = todos.split(',').map(id => mongoose.Types.ObjectId(id));
user.todos.push(...objectIds)
await user.save()
console.log("user: ", JSON.stringify(user));
if (_.isEmpty(user)) {
res.status(500).json({
message: 'unable to create user'
})
}
res.json(user);
} catch (err) {
res.status(500).json({
message: 'unable to create user',
err: JSON.stringify(err)
})
}
});
router.get("/", async function (req, res) {
try {
let user = await User.find().populate('todos');
console.log("user: ", JSON.stringify(user));
if (_.isEmpty(user)) {
res.status(500).json({
message: 'unable to find user'
})
}
res.json(user);
} catch (err) {
res.status(500).json({
message: 'unable to find user',
err: JSON.stringify(err)
})
}
});
module.exports = router;
Check out the attached screenshot, the user record now contains the todos assigned to it.
If you want checkout the working code, please visit this repo that i created!!.
Hope this help.Cheers!!

Mongoose can't find my data from database but database has been connected

It has show "Database connected" , I think database has already connected to mongodb server already. However ,there has nothing to be console log. Data is null here and nothing is print on my page.
Here is my code.
index.js
var userModel = require('../models/userModel.js');
router.get('/', function (req, res, next) {
mongoose.connect('mongodb://localhost:27017/db', { useNewUrlParser: true }, function (err) {
if (err) console.log("connect fail");
console.log("Database connected");
});
})
userModel.find({}, function (err, data) {
if (err) {
return console.log(err)
}
console.log(data);
res.render('index', {
title: 'Account', user: data, loginStatus: isLogin
});
})
models/userModel.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
var userSchema = new mongoose.Schema({
name: String,
account: String,
password: String,
email: String
});
var model = mongoose.model('user', userSchema);
module.exports = model;
The problem in this is you're connecting database on api call which should never be the case. You should always do it in the beginning and call the collections at the endpoints. Now whenever you'll call '/' check server console.
var userModel = require('../models/userModel.js');
mongoose.connect('mongodb://localhost:27017/db', { useNewUrlParser: true }, function (err) {
if (err) console.log("connect fail");
console.log("Database connected");
});
router.get('/', function (req, res, next) {
userModel.find({}, function (err, data) {
if (err) {
return console.log(err)
}
console.log(data);
res.render('index', {
title: 'Account', user: data, loginStatus: isLogin
});
})
})

(mongoose) put method does not work

I am creating web API using mongoose.
POST and GET work, but I have no idea how to implement PUT method in mongoose.
Here is what I created:
board.js
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const config = require('../config/database');
const BoardSchema = mongoose.Schema({
_id: {
type: String
},
position: {
type: [String]
}
});
const Board = module.exports = mongoose.model('boards', BoardSchema);
module.exports.getBoardById = function (id, callback)
{
Board.findById(id, callback);
}
module.exports.addBoard = function (newBoard, callback)
{
newBoard.save(callback);
}
module.exports.updateBoard = function (newBoard, callback)
{
newBoard.save(callback);
}
users.js
router.put('/board/:id', (req, res, next) =>
{
let newBoard = new Board({
_id: req.params.id,
position: req.body.position
});
Board.updateBoard(newBoard, (err, board) =>
{
if (err)
{
res.json({ newBoard: newBoard, success: false, msg: "Failed to update board" });
}
else
{
res.json({ newBoard: newBoard, success: true, msg: "Board added" });
}
});
});;
Here, in the board.js, I created methods for adding a new board and updating to existing board. .addBoard is working correctly and am able to test it using Postman. But, .updateBoard adds the data when the data does not exist, but does not update any data and returns false as response (just like POST does). Is there any way I can make the PUT method works?
Thank you!
Please let me know if this works for you! I want to introduce you to http://mongoosejs.com/docs/api.html#findbyidandupdate_findByIdAndUpdate
router.put('/board/:id', (req, res) => {
const {id: _id} = req.params // Assigning id to _id which is a es6 feature. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
const {position} = req.body
const newBoard = {
_id,
position
}
Board.findByIdAndUpdate(
_id,
newBoard,
(err, updatedBoard) => {
if (err) {
res.json({
newBoard,
success: false,
msg: 'Failed to update board'
})
} else {
res.json({newBoard, success: true, msg: 'Board added'})
}
}
)
})
why are you using save method while updating?
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const config = require('../config/database');
const BoardSchema = mongoose.Schema({
_id: {
type: String
},
position: {
type: [String]
}
});
const Board = module.exports = mongoose.model('boards', BoardSchema);
module.exports.getBoardById = function (id, callback)
{
Board.findById(id, callback);
}
module.exports.addBoard = function (newBoard, callback)
{
newBoard.save(callback);
}
module.exports.updateBoard = function (condition, update, callback)
{
Board.update(condition,update,callback);
}
in controller
router.put('/board/:id', (req, res, next) =>
{
let newBoard = new Board({
_id: req.params.id,
position: req.body.position
});
Board.updateBoard({ _id: req.params.id } ,newBoard, (err, board) =>
{
if (err)
{
res.json({ newBoard: newBoard, success: false, msg: "Failed to update board" });
}
else
{
res.json({ newBoard: newBoard, success: true, msg: "Board added" });
}
});
});
try this.
As you are using req.body i think you are trying to call a put request from a form (sometimes happens with AJAX requests also). For doing that use method-overide. And set the xhr header as given in the documentation. This will surely work.

How to create a Mongo database to Node.js?

I have created the backend operations using Node.js / MongoDB and created services for them, but when I run the server it indicates that I've connected successfully, but I can't see a database in Robo 3T.
Here is what I coded:
DBConfig.js
var mongoose = require ('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
name : {
type : String,
require : true
},
address : {
type : String,
require : true
}
});
mongoose.model("User", UserSchema);
module.exports = mongoose;
mongoose.connect('mongodb://127.0.0.1:27017/TestDB4', function(err){
if(err)
{
console.log(err);
process.exit(-1);
}
console.log("Connected to the db")
});
I can't see an error. Why isn't the DB created?
check your User.Controller.js as this below,
var mongoose = require('../DBSchema/SchemaMapper');
var UserSchema = mongoose.model('User');
var UserController = function(){
this.insert = (data) => {
return new Promise((resolve, reject) => {
var user = new UserSchema({
userName: data.userName,
password: data.password
});
user.save().then(() => {
resolve({status: 200, message: "Added new user"});
}).catch(err => {
reject({status: 500, message: "Error:- "+err});
})
})
}
this.update = (id, data) => {
return new Promise((resolve, reject) => {
UserSchema.update({_id: id}, data).then(() => {
resolve({status: 200, message: "update user"});
}).catch(err => {
reject({status: 500, message: "Error:- " + err});
})
})
}
this.searchAll = () => {
return new Promise((resolve, reject) => {
UserSchema.find().exec().then((data) => {
resolve({status: 200, data: data});
}).catch(err => {
reject({status: 500, message: "Error:- " + err});
})
})
}
this.search = (id) => {
return new Promise((resolve, reject) => {
UserSchema.find({_id:id}).exec().then(user => {
resolve({status: 200, data: user});
}).catch(err => {
reject({status: 500, message: "Error:- " + err});
})
})
}
this.delete = (id) => {
return new Promise((resolve, reject) => {
UserSchema.remove({_id:id}).then(() => {
resolve({status: 200, message: "remove user"});
}).catch(err => {
reject({status: 500, message:"Error:- " + err});
})
})
}
}
module.exports = new UserController();
And User.Route.js as below,
var express = require('express');
var router = express.Router();
var Controller = require('./User.Controller');
router.post('/', (req, res) => {
Controller.insert(req.body).then(data => {
res.status(data.status).send({message: data.message});
}).catch(err => {
res.status(err.status).send({message: err.message});
})
});
router.put('/:id', (req, res) => {
Controller.update(req.params.id, req.body).then(data => {
res.status(data.status).send({message: data.message});
}).catch(err => {
res.status(err.status).send({message: err.message});
})
});
router.get('/', (req, res) => {
Controller.searchAll().then(data => {
res.status(data.status).send({data: data.data});
}).catch(err => {
res.status(err.status).send({message: err.message});
});
});
router.get('/:id', (req, res) => {
Controller.search(req.params.id).then(data => {
res.status(data.status).send({data: data.data});
}).catch(err => {
res.status(err.status).send({message: err.message});
});
});
router.delete('/:id', (req, res) => {
Controller.delete(req.params.id).then(data => {
res.status(data.status).send({message: data.message});
}).catch(err => {
res.status(err.status).send({message: err.message});
})
})
module.exports = router;
check your app.js as follows
const Express = require("express");
const BodyParser = require("body-parser");
const Routes = require("./Routes");
const Cors = require("cors");
const app = Express();
app.use(Cors());
app.use(BodyParser.urlencoded({ extended: false }));
app.use(BodyParser.json());
app.use('/', Routes);
app.listen(8083, 'localhost', (err) => {
if(err) {
console.log(err);
process.exit(-1);
}
console.log("Server listen port 8083");
});
and Routes.js as follows,
var Express = require("express");
var Routes = Express.Router();
var UserRoute = require('./src/User/User.Route');
var CommentRoute = require('./src/Comment/Comment.Route');
Routes.use('/user/', UserRoute);
Routes.use('/comment/', CommentRoute);
module.exports = Routes;

REST api redundancy

So i am creating a RESTFul api for my application using Node.js
However it is starting to feel abit redundant and therefore i am wondering if i am doing it correctly.
So when ever i have a new model i do the following:
First i create a file equal to the table name in the database (lets take a table called team as an example):
// IMPORT ROUTES
// =============================================================================
module.exports = function (express, sequelize, router) {
router.route('/team');
var DataTypes = require("sequelize");
var crypto = require('crypto');
// on routes that end in /Teams
// ----------------------------------------------------
router.route('/api/team')
// create a Team (accessed at POST http://localhost:8080/api/Teams)
.post(function (req, res) {
var name = req.body.name; //bodyParser does the magic
var academy_id = req.body.academy_id;
var team = Team.build({name: name, academy_id: academy_id});
team.add(function (success) {
res.json({message: 'Team created!'});
},
function (err) {
res.status(err).send(err);
});
})
// get all the Teams (accessed at GET http://localhost:8080/api/Teams)
.get(function (req, res) {
var team = Team.build();
team.retrieveAll(function (Teams) {
if (Teams) {
res.json(Teams);
} else {
res.status(401).send("Team not found");
}
}, function (error) {
res.status("Team not found").send('Team not found');
});
});
var Team = sequelize.define('team', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
academy_id: DataTypes.INTEGER
}, {
freezeTableName: true,
instanceMethods: {
retrieveAll: function (onSuccess, onError) {
Team.findAll({}, {raw: true})
.ok(onSuccess).error(onError);
},
retrieveById: function (Team_id, onSuccess, onError) {
Team.find({where: {id: Team_id}}, {raw: true})
.success(onSuccess).error(onError);
},
add: function (onSuccess, onError) {
var Teamname = this.name;
var password = this.password;
var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');
Team.build({name: Teamname, name: password})
.save().ok(onSuccess).error(onError);
},
updateById: function (Team_id, onSuccess, onError) {
var id = Team_id;
var Teamname = this.Teamname;
var password = this.password;
var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');
Team.update({Teamname: Teamname, password: password}, {where: {id: id}})
.success(onSuccess).error(onError);
},
removeById: function (Team_id, onSuccess, onError) {
Team.destroy({where: {id: Team_id}}).success(onSuccess).error(onError);
},
retrieveByAcademyId: function(academy_id, onSuccess, onError)
{
Team.findAll({where: {academy_id: academy_id}}, {raw: true})
.ok(onSuccess).error(onError);
}
}
}
),
academy = sequelize.define('academy', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
organization_id: DataTypes.INTEGER,
status_id: DataTypes.INTEGER
});
Team.belongsTo(academy,{foreignKey: 'academy_id'});
// on routes that end in /Teams/:Team_id
// ----------------------------------------------------
router.route('/api/team/:team_id')
// update a Team (accessed at PUT http://localhost:8080/api/Teams/:Team_id)
.put(function (req, res) {
var team = Team.build();
team.name = req.body.name;
team.academy_id = req.body.academy_id;
team.updateById(req.params.id, function (success) {
console.log(success);
if (success) {
res.json({message: 'Team updated!'});
} else {
res.send(401, "Team not found");
}
}, function (error) {
res.send("Team not found");
});
})
// get a Team by id(accessed at GET http://localhost:8080/api/Teams/:Team_id)
.get(function (req, res) {
var team = Team.build();
team.retrieveById(req.params.team_id, function (teams) {
if (teams) {
res.json(teams);
} else {
res.status(401).send("Team not found");
}
}, function (error) {
res.send("Team not found");
});
})
// delete a Team by id (accessed at DELETE http://localhost:8080/api/Teams/:Team_id)
.delete(function (req, res) {
var team = Team.build();
team.removeById(req.params.id, function (teams) {
if (teams) {
res.json({message: 'Team removed!'});
} else {
res.status(401).send("Team not found");
}
}, function (error) {
res.send("Team not found");
});
});
router.route('/api/academyTeam/:academy_id')
.get(function (req, res) {
var team = Team.build();
team.retrieveByAcademyId(req.params.academy_id, function (teams) {
if (teams) {
res.json(teams);
} else {
res.status(401).send("Team not found");
}
}, function (error) {
res.send("Team not found");
});
});
return router;
};
Then afterwards i add the following line to my server.js:
app.use(team_model);
then i repeat the action for the next table.
Am i doing it right or is there a way to optimize things?

Resources