Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Database with Node.js?

I am trying to make login in Node.js. but i am confused how to use database.And my Login.html & database.js is like. http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local So please give me proper idea.

<form action="/login" method="post">
    <div class="form-group">
        <label>Email</label>
        <input type="text" class="form-control" name="email">
    </div>
    <div class="form-group">
        <label>Password</label>
        <input type="password" class="form-control" name="password">
    </div>
    <button type="submit" class="btn btn-warning btn-lg">Login</button>

database.js:

module.exports = {
    'url' : 'your-database-here' 

};
like image 310
Rohit Dhiman Avatar asked Aug 23 '26 15:08

Rohit Dhiman


1 Answers

In your server.js should be this bit of code (assuming you are following the tutorial you linked exactly).

var configDB = require('./config/database.js');
mongoose.connect(configDB.url);

According to that code, your database.js file must be in a config folder (rather then in the same folder as server.js). So make sure you are doing that.

Next, place your mongodb url like so in your database.js.

module.exports = {
    'url' : 'mongodb://<user>:<pass>@mongo.onmodulus.net:27017/Mikha4ot'
};

Make sure to replace <user> with your username and <pass> with your password.

Need help getting a mongoDB database for yourself?

I will help you.

1:

Go to Mongolab's signup page.

2:

Once signed up, tap the button.

3:

Choose the Cloud provider you want. (I just kept Amazon since it was already selected)

4:

When you get to the Plan section, make sure you choose the free plan (unless of course you want to pay).

5:

Once you create the database, click into it!

6:

Now you will find the connection url that you want to put in your module.exports

like image 75
aleclarson Avatar answered Aug 26 '26 05:08

aleclarson