1

I'm a beginner to nodejs and i'm developing a REST api to interact with mongodb. I used express and mongoose as explained in following tutorial :

https://codeforgeek.com/2015/08/restful-api-node-mongodb/

I have setup mongo db and here is my server code in node js :

var express     =   require("express");
var app         =   express();
var bodyParser  =   require("body-parser");
var router      =   express.Router();
var mongoOp     =   require("./models/mongo");
//var user        =   require("./Entities/User");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({"extended" : false}));
router.get("/",function(req,res){
    res.json({"error" : false,"message" : "Hello World"});
});
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);
        });
    })
    .post(function(req,res){
        var db = new mongoOp();
        var response = {};
        // fetch email and password from REST request.
        // Add strict validation when you use this in Production.
        db.userEmail = req.body.email;
        // Hash the password using SHA1 algorithm.
        db.userPassword =  require('crypto')
                          .createHash('sha1')
                          .update(req.body.password)
                          .digest('base64');
        db.save(function(err){
        // save() will run insert() command of MongoDB.
        // it will add new data in collection.
            if(err) {
                response = {"error" : true,"message" : "Error adding data"};
            } else {
                response = {"error" : false,"message" : "Data added"};
            }
            res.json(response);
        });
    });
app.use('/',router);
app.listen(3000);
console.log("Listening to PORT 3000");

and here is my mongo module

var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/Appdb");
//Create instance of Schema
var mongoSchema = mongoose.schema;
//Create Schema
var userSchema = {
    "userEmail":String,
    "userPassword":String
}
//Create model if not exists.
module.export = mongoose.model('userLogin',userSchema);

When i used RESTClient (an addon in firefox to test REStful application) it gives following error :

TypeError: object is not a function at E:\node\server.js:30:18
at Layer.handle [as handle_request] (E:\node\node_modules\express\lib\router\layer.js:95:5) .........

enter image description here

Can anyone help me please...

1 Answer 1

1

module.exports = mongoose.model('userLogin',userSchema);

use exports not export

Sign up to request clarification or add additional context in comments.

5 Comments

Now it give me TypeError: Not a string or buffer at TypeError (native) at Hash.update (crypto.js:119:16) at E:\node\server.js:38:28 error...
db.userPassword = require('crypto') .createHash('sha1') .update(req.body.password) .digest('base64'); something wrong with this. can u check your req.body.password is atleast some string using console.log
@Piysuh i display as undifined , is that mean when i pass password via RestClient is not passed to the api?
req.body is not coming out to be what you are passing through rest client I see you are using body parser which is correct .
also make sure in your rest client in your request headers data is being sent as application/json This is the only issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.