HELP ME, PLEASE

Question

I am a 1st year B.Tech Computer Science student. I want help in my assignment. Please get it solved as soon as possible.

Build a Restful API using Node and MangoDB

 

Thank you so much

solved 0
Computer Dilip 5 months 1 Answer 111 views 0

Answer ( 1 )

  1. Server.js

    //Welcome to hell world of programming
    //Copyright © <2017>, <Adarsh Mohan>
    //I am not responsible of this code.
    //When I wrote this, only God and I understood what I was doing.
    //Now, God only knows.
    //I don’t want to study, still I am studying so seriously 🙁 🙁 Abhi tak MOH-MAAYA me phasa hun 🙁 🙁
    {
    “name”: “nodeMongo”,
    “version”: “1.0.0”,
    “description”: “Node.js and MongoDb tutorial.”,
    “main”: “Server.js”,
    “scripts”: {
    “test”: “mocha”
    },
    “repository”: {
    “type”: “git”,
    “url”: “http://kailashafoundation.org”
    },
    “keywords”: [
    “Node.js”,
    “mongoDb”
    ],
    “author”: “Adarsh M”,
    “license”: “ISC”,
    “bugs”:
    “dependencies”: {
    “body-parser”: “^1.13.3”,
    “express”: “^4.13.3”,
    “mongoose”: “^4.1.2”
    }
    }
    var express = require(“express”);
    var app = express();
    var bodyParser = require(“body-parser”);
    var router = express.Router();

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({“extended” : false}));

    router.get(“/”,function(req,res){
    res.json({“error” : false,”message” : “Hello World”});
    });

    app.use(‘/’,router);
    app.listen(3000);
    console.log(“Listening to PORT 3000”);
    var mongoose = require(“mongoose”);
    mongoose.connect(‘mongodb://localhost:27017/demoDb’);
    var mongoSchema = mongoose.Schema;
    var userSchema = {
    “userEmail” : String,
    “userPassword” : String
    };
    mongoose.model(‘user_login’,userSchema);
    var mongoose = require(“mongoose”);
    mongoose.connect(‘mongodb://localhost:27017/demoDb’);
    // create instance of Schema
    var mongoSchema = mongoose.Schema;
    // create schema
    var userSchema = {
    “userEmail” : String,
    “userPassword” : String
    };
    // create model if not exists.
    module.exports = mongoose.model(‘userLogin’,userSchema);
    var express = require(“express”);
    var app = express();
    var bodyParser = require(“body-parser”);
    var mongoOp = require(“./models/mongo”);
    var router = express.Router();
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({“extended” : false}));
    router.get(“/”,function(req,res){
    res.json({“error” : false,”message” : “Hello World”});
    });

    //route() will allow you to use same path for different HTTP operation.
    //So if you have same URL but with different HTTP OP such as POST,GET etc
    //Then use route() to remove redundant code.

    router.route(“/users”)
    .get(function(req,res){
    var response = {};
    mongoOp.find({},function(err,data){
    // Mongo command to fetch all data from collection.
    if(err) {
    response = {“error” : true,”message” : “Error fetching data”};
    } else {
    response = {“error” : false,”message” : data};
    }
    res.json(response);
    });
    });

    app.use(‘/’,router);

    app.listen(3000);
    console.log(“Listening to PORT 3000″);

    To test the API, open up and hit “localhost:3000/users”

    Best answer

Leave an answer

Browse