Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60b7d2c

Browse files
committed
Read me added
1 parent 67e19ba commit 60b7d2c

File tree

1 file changed

+135
-1
lines changed

1 file changed

+135
-1
lines changed

‎README.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,135 @@
1-
# node-mysql-express-template-v1
1+
# node-mysql-express-template-v1
2+
3+
Node mysql express template version 1 is a easy to use template, for any start-ups to download and get started with. It can be pushed to a cloud service in a matter of seconds.
4+
5+
## Getting started!
6+
> Prequisites!
7+
8+
You need to have a computer running the following:
9+
- [NODE.JS](https://nodejs.org/en/)
10+
- [MYSQL](https://www.mysql.com/)
11+
12+
> Step 1
13+
14+
1) Git clone the repo to your computer
15+
2) Run the following command `npm install`
16+
3) Go to the db.js file in the root and configure the database settings
17+
4) Copy the sql create statement from `database_create.sql` and run it inside the mysql terminal or phpmyadmin or mysqlworkbench, to create the users table.
18+
5) Run the following command `node index.js`
19+
20+
Thanks it! You have a running platform!
21+
22+
### Tech
23+
The repo is using the following tech, I will break them down one by one for ease of use.
24+
25+
* [Node JS] - The server.
26+
* [Express JS] - The framework that fits over Node.js.
27+
* [MYSQL] - The database that is used with this template.
28+
* [Json web token] - A token auth system to show how to use auth if you want to connect front and backend.
29+
* [Bcrypt] - A framework that encrypts password and can be used to encrypt other things too.
30+
31+
32+
### Break down
33+
> `index.js`
34+
35+
The code below takes care of calling all the database functions to you index.js file.
36+
```sh
37+
const {saveUser,findUser, UpdateToken} = require('./models/user');
38+
```
39+
Authenticate is the middle that is being used to authenticate the front-end or user
40+
Via the token, so all the functions you would want to restrict can be done with the below method.
41+
```sh
42+
const {authenticate} = require('./middleware/authenticate');
43+
```
44+
There are three functions in the index.js file for the app.
45+
A `POST` request , `GET` request, and an authentication function to show how authentication middle ware is used.
46+
The function below just renders the index.html file from the public folder in the root directory and shows it to the screen when you type
47+
localhost:3000
48+
```sh
49+
app.get('/', function(req, res, next) {
50+
res.render('index');
51+
});
52+
```
53+
The function below `/createUser`, creates a new user and stores it in the db.
54+
The function calls `saveUser` from the `models` and then saves the user, and returns the token and the email back to the user. You can notice that `Promises` are being used.
55+
If the function fails to register it returns a 400 error to the user.
56+
If you want to learn more click: [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
57+
```sh
58+
/* The function below
59+
app.post('/createUser',(req,res,next)=>{
60+
saveUser(req.body).then((result)=>{
61+
return res.header('x-auth', result.token).send({email : result.email});
62+
}).catch((e)=>{
63+
return res.status(400).send(e);
64+
});
65+
});
66+
```
67+
The function below shows how the `authenticate` middle wear can be used to identify the user and allow the user to access certain functions that you want to protect.
68+
For example here I am just using the word authenticate to block the `/get/me` from being used without any token and just simply passing the users information back.
69+
```sh
70+
app.get('/get/me',authenticate,(req,res,next)=>{
71+
res.send(req.user);
72+
});
73+
```
74+
> `middleware/authenticate.js`
75+
76+
First we get the `getUserByToken` function from the model and pass in the token that we get back to the model, the model searches for the same token and then passes the user information back and calls `next()` to let node know that the user is validated so the user can access the function.
77+
```sh
78+
const {getUserByToken} = require('../models/user');
79+
var authenticate = (req,res,next)=>{
80+
var token = req.header('x-auth');
81+
getUserByToken(token).then((result)=>{
82+
req.user = result;
83+
req.token = token;
84+
next();
85+
}).catch((e)=>{
86+
return res.status(401).send();
87+
});
88+
}
89+
```
90+
91+
> `models/user.js`
92+
93+
The model has all sql functionality. It returns a `Promise` in all of the calls.
94+
But for example lets take the function that creates a new user.
95+
1) It uses bcrypt to encrypt the users password
96+
2) It juses json webtoken to encrypt the users information and provide it back as token
97+
3) It inserts to the user table
98+
4) returns back the users table that just got inserted.
99+
100+
```sh
101+
saveUser = (userinfo) => new Promise((resolve,reject)=>{
102+
var salt = bcrypt.genSaltSync(10);
103+
var hash = bcrypt.hashSync(userinfo.password, salt);
104+
105+
userinfo.password = hash;
106+
userinfo.token = jwt.sign({Owner : userinfo.Owner},'secretkey');
107+
108+
db.query('INSERT INTO user SET ?',userinfo,function(error,results,fields){
109+
if(error){
110+
reject();
111+
}else{
112+
resolve(userinfo);
113+
}
114+
})
115+
});
116+
```
117+
### Cloud
118+
To get it to the cloud either click the button below to deploy to heroku
119+
or just manually change the database to get a db in the cloud and push it to the server of your choice ( heroku, aws, digital ocean)
120+
121+
### Final
122+
123+
To know more about how to setup a perfect architecture. Read the article below.
124+
[The perfect architecture](https://medium.com/@AuroraXFi/the-perfect-technical-architecture-for-an-i-t-startup-97bec70f3c9e)
125+
That's about!
126+
127+
### Author
128+
129+
- Masnad
130+
131+
License
132+
MIT
133+
134+
**Free Software, Hell Yeah!**
135+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /