fetch api based on id nodejs and mongodb
JavaScript
// api-routes.js// Initialize express routerlet router = require('express').Router();// Set default API responserouter.get('/', function (req, res) { res.json({ status: 'API Its Working', message: 'Welcome to RESTHub crafted with love!', });});// Import contact controllervar contactController = require('./contactController');// Contact routesrouter.route('/contacts') .get(contactController.index) .post(contactController.new);router.route('/contacts/:contact_id') .get(contactController.view) .patch(contactController.update) .put(contactController.update) .delete(contactController.delete);// Export API routesmodule.exports = router;// contactController.js// Import contact modelContact = require('./contactModel');// Handle index actionsexports.index = function (req, res) { Contact.get(function (err, contacts) { if (err) { res.json({ status: "error", message: err, }); } res.json({ status: "success", message: "Contacts retrieved successfully", data: contacts }); });};// Handle create contact actionsexports.new = function (req, res) { var contact = new Contact(); contact.name = req.body.name ? req.body.name : contact.name; contact.gender = req.body.gender; contact.email = req.body.email; contact.phone = req.body.phone;// save the contact and check for errors contact.save(function (err) { // if (err) // res.json(err);res.json({ message: 'New contact created!', data: contact }); });};// Handle view contact infoexports.view = function (req, res) { Contact.findById(req.params.contact_id, function (err, contact) { if (err) res.send(err); res.json({ message: 'Contact details loading..', data: contact }); });};// Handle update contact infoexports.update = function (req, res) {Contact.findById(req.params.contact_id, function (err, contact) { if (err) res.send(err);contact.name = req.body.name ? req.body.name : contact.name; contact.gender = req.body.gender; contact.email = req.body.email; contact.phone = req.body.phone;// save the contact and check for errors contact.save(function (err) { if (err) res.json(err); res.json({ message: 'Contact Info updated', data: contact }); }); });};// Handle delete contactexports.delete = function (req, res) { Contact.remove({ _id: req.params.contact_id }, function (err, contact) { if (err) res.send(err);res.json({ status: "success", message: 'Contact deleted' }); });};// contactModel.jsvar mongoose = require('mongoose');// Setup schemavar contactSchema = mongoose.Schema({ name: { type: String, required: true }, email: { type: String, required: true }, gender: String, phone: String, create_date: { type: Date, default: Date.now }});// Export Contact modelvar Contact = module.exports = mongoose.model('contact', contactSchema);module.exports.get = function (callback, limit) { Contact.find(callback).limit(limit);}// Import expresslet express = require('express');// Import Body parserlet bodyParser = require('body-parser');// Import Mongooselet mongoose = require('mongoose');// Initialise the applet app = express();// Import routeslet apiRoutes = require("./api-routes");// Configure bodyparser to handle post requestsapp.use(bodyParser.urlencoded({ extended: true}));app.use(bodyParser.json());// Connect to Mongoose and set connection variablemongoose.connect('mongodb://localhost/resthub', { useNewUrlParser: true});var db = mongoose.connection;// Added check for DB connectionif(!db) console.log("Error connecting db")else console.log("Db connected successfully")// Setup server portvar port = process.env.PORT || 8080;// Send message for default URLapp.get('/', (req, res) => res.send('Hello World with Express'));// Use Api routes in the Appapp.use('/api', apiRoutes);// Launch app to listen to specified portapp.listen(port, function () { console.log("Running RestHub on port " + port);});
Also in JavaScript:
- Title
- changing attributes using javascript
- Category
- JavaScript
- Title
- convert json object to array javascript
- Category
- JavaScript
- Title
- bootstrap 4 hide modal via javascript
- Category
- JavaScript
- Title
- how to output to console java
- Category
- JavaScript
- Title
- create array javascript
- Category
- JavaScript
- Title
- javaScript Age in Dog years //write a function that takes your age and returns it to you in dog years - they say that 1 human year is equal to seven dog years function dog Years() javaScript
- Category
- JavaScript
- Title
- how to make a inventory in js
- Category
- JavaScript
- Title
- how to include local image files in javascript object
- Category
- JavaScript
- Title
- express js list all routes
- Category
- JavaScript
- Title
- conditional rendering in react
- Category
- JavaScript
- Title
- javascript clear file input
- Category
- JavaScript
- Title
- how to run different node app on server different domains
- Category
- JavaScript
- Title
- how to make a random number guessing game in javascript
- Category
- JavaScript
- Title
- how to deploy react app in tomcat server
- Category
- JavaScript
- Title
- change width in js
- Category
- JavaScript
- Title
- array unique values javascript
- Category
- JavaScript
- Title
- angular add debounce time before putting valu in subject next
- Category
- JavaScript
- Title
- add webpack to react project
- Category
- JavaScript
- Title
- dont starve together
- Category
- JavaScript
- Title
- how to parse json
- Category
- JavaScript
- Title
- credit card mask js
- Category
- JavaScript
- Title
- how to make a dictionary javascript
- Category
- JavaScript
- Title
- discord.js multiple embeds
- Category
- JavaScript
- Title
- javascript append to object
- Category
- JavaScript
- Title
- how to rescale a canvas in html5 without blurrriness
- Category
- JavaScript
- Title
- angular material table
- Category
- JavaScript
- Title
- append before jquery
- Category
- JavaScript
- Title
- firebase configuration on functions
- Category
- JavaScript
- Title
- discord.js send message to channel
- Category
- JavaScript
- Title
- javascript base64 encode
- Category
- JavaScript
- Title
- install php7 runtime brackets
- Category
- JavaScript
- Title
- handling event in jsx
- Category
- JavaScript
- Title
- "npm supertest
- Category
- JavaScript
- Title
- airbnb and eslint react native
- Category
- JavaScript
- Title
- enable disable click on div jquery
- Category
- JavaScript
- Title
- axios post with header
- Category
- JavaScript
- Title
- a simple javascript calculator
- Category
- JavaScript
- Title
- input output ionic
- Category
- JavaScript
- Title
- if checkbox checked jquery value 1
- Category
- JavaScript
- Title
- how to get a toggle button to do different js functions
- Category
- JavaScript
- Title
- how to split two digit number in javascript
- Category
- JavaScript
- Title
- discord js bot leave voice channel
- Category
- JavaScript
- Title
- how to create seeds in sequelise
- Category
- JavaScript
- Title
- how to make an express server
- Category
- JavaScript
- Title
- ad data to js object
- Category
- JavaScript
- Title
- google sign in in firebase react
- Category
- JavaScript
- Title
- faker js
- Category
- JavaScript
- Title
- angular list contains property
- Category
- JavaScript
- Title
- angular add object to array
- Category
- JavaScript
- Title
- how to animate array oy react elements
- Category
- JavaScript
- Title
- how to get textedit on mac without download
- Category
- JavaScript
- Title
- how to convert array to uppercase in javascript
- Category
- JavaScript
- Title
- how to add text to h2 with jquery
- Category
- JavaScript
- Title
- const{}=
- Category
- JavaScript
- Title
- install nuxt js
- Category
- JavaScript
- Title
- google script get name of google form
- Category
- JavaScript
- Title
- button style not working react native
- Category
- JavaScript
- Title
- how to remove session data on tab close in redux\
- Category
- JavaScript
- Title
- funciones invocan a funciones javascript
- Category
- JavaScript
- Title
- javascript add option element to select
- Category
- JavaScript
- Title
- convert number to k m b javascript
- Category
- JavaScript
- Title
- add and remove class in jquery
- Category
- JavaScript
- Title
- email regex
- Category
- JavaScript
- Title
- enzyme check state
- Category
- JavaScript
- Title
- addeventlistener on document in react js
- Category
- JavaScript
- Title
- javascript click button by id
- Category
- JavaScript
- Title
- how to write a program that alerts the first 3 letters of the current day in javascript
- Category
- JavaScript
- Title
- add all elements in array javascript
- Category
- JavaScript
- Title
- add to json object javascript
- Category
- JavaScript
- Title
- javascript change attribute
- Category
- JavaScript
- Title
- express validator always return error
- Category
- JavaScript
- Title
- how to add onclick to child element created javascript
- Category
- JavaScript
- Title
- function click anywhere javascript
- Category
- JavaScript
- Title
- how to link javascript to html
- Category
- JavaScript
- Title
- delete cookies by domain javascript
- Category
- JavaScript
- Title
- get utc time javascript
- Category
- JavaScript
- Title
- how to get a random element of an array javascript
- Category
- JavaScript
- Title
- firestore batch add array
- Category
- JavaScript
- Title
- day to week
- Category
- JavaScript
- Title
- clearinterval javascript
- Category
- JavaScript
- Title
- close bootstrap modal with javascript
- Category
- JavaScript
- Title
- javascript add element to serialized form array
- Category
- JavaScript
- Title
- access session data from ejs view
- Category
- JavaScript
- Title
- event listener on parent not on childjavascript
- Category
- JavaScript
- Title
- detect a click outside an element javascript
- Category
- JavaScript
- Title
- change text of element js
- Category
- JavaScript
- Title
- find index of object in array javascript
- Category
- JavaScript
- Title
- For-each over an array in JavaScript
- Category
- JavaScript
- Title
- comparing two arrays in javascript returning differences
- Category
- JavaScript
- Title
- how to define cardTitle background image in mdl in reactjs
- Category
- JavaScript
- Title
- convert dictionary to json serialization swift 4
- Category
- JavaScript
- Title
- A cookie associated with a cross-site resource at http://placeholder.com/ was set without the `SameSite` attribute.
- Category
- JavaScript
- Title
- boucle foreach js
- Category
- JavaScript
- Title
- how to count seconds in javascript
- Category
- JavaScript
- Title
- dom create element
- Category
- JavaScript
- Title
- how to calculate balance automatically using javascript and php using cash paid
- Category
- JavaScript
- Title
- datatables server side
- Category
- JavaScript
- Title
- javascript array pop
- Category
- JavaScript
- Title
- for each js
- Category
- JavaScript
- Title
- ajax .put
- Category
- JavaScript