Need assistance with a REST API using Express? Let's build an Express application that operates on port 3000 and links to a database. The database should contain a user collection with the following user details:
a) userId - "user1", firstName - "Akshay", lastName - "Kumar", email - "[email protected]"
b) userId - "user2" firstName - "Rajnikanth", lastName - "", email - "[email protected]"
Please note that including the database part is not necessary (provided for context).
The application requires the following APIs: /users - returns JSON of all users in the DB /users/:userId - returns the object of a single user based on the userId passed
To complete this, modify the following files: response.js app.js
Create a fully operational REST API capable of producing two types of responses - a) Error response - {‘isError’:true,'status’: 500,'errorMessage':’Some error message’,successMessage:null} b) Success response - {‘isError’:false,'status’: 200,'errorMessage':null,successMessage:’Some result’}
PLEASE NOTE: app.listen() function will be handled by the system.
I have tried implementing this solution, but encountered logical errors. Unfortunately, I am unable to test the code as it needs to be submitted online with only syntax error feedback from the website compiler.
All JavaScript files are provided below:
app.js
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const UserModel = require('./User.js');
let db = mongoose.connect('mongodb://testuser:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98e8f9ebebeff7eafca9aaabd8fceba9aca1aaadaab6f5f4f9fab6fbf7f5">[email protected]</a>:49252/assignment', { useMongoClient: true });
const responseLib = require('./responseLib');
app.get('/users', function(req, res, err) {
UserModel.find(function(err, result){
if(err) {
let apiResponse = response.generateResponse(true, 500, "Some error message", null);
res.send(apiResponse);
} else {
let apiResponse = response.generateResponse(false, 200, "Some result", result);
res.send(apiResponse);
}
});
});
app.get('/users/:userId', function(req, res, err){
BlogModel.findOne({ 'blogId': req.params.userId }, (err, result) => {
if (err) {
let apiResponse = response.generateResponse(true, 500, "Some error message", null);
res.send(apiResponse);
} else {
let apiResponse = response.generateResponse(false, 200, "Some result", result);
res.send(apiResponse);
}
});
});
module.exports = app;
User.js
// importing mongoose module
// importing mongoose module
const mongoose = require('mongoose')
// import schema
const Schema = mongoose.Schema;
let userSchema = new Schema(
{
userId: {
type: String,
unique: true
},
firstName: {
type: String,
default: ''
},
lastName: {
type: String,
default: ''
},
email: {
type: String,
default: ''
}
}
)
mongoose.model('User', userSchema);
module.exports = mongoose.model('User', userSchema)
responseLib.js
let generateResponse = (isError,status,errorMessage,successMessage) =>{
let response = {
isError : isError,
status : status,
errorMessage : errorMessage,
successMessage : successMessage
}
return response
};// end generate response.
module.exports = {generateResponse:generateResponse}