Rohit Dhiman
Rohit Dhiman

Reputation: 125

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' 

};

Upvotes: 3

Views: 785

Answers (3)

aleclarson
aleclarson

Reputation: 19035

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

Upvotes: 2

Quentin
Quentin

Reputation: 943128

From the tutorial you link to:

Fill this in with your own database. If you don’t have a MongoDB database lying around, I would suggest going to Modulus.io and grabbing one. Once you sign up (and you get a $15 credit for signing up), you can create your database, grab its connection url, and place it in this file.

Otherwise you will need to download MongoDB and run your own instance of it.

Alternatively, rewrite the database code to use a different kind of database and find an NPM module to interface with it.

Upvotes: 0

Jim Jeffries
Jim Jeffries

Reputation: 10081

It depends which database you are using. Different databases often have different ways of accessing them. For example, if you are using postgres you can use node-postgres from npm.

Upvotes: 1

Related Questions