Skip to content

Commit fe59b45

Browse files
author
App Generator
committed
v0.0.3 - Complete Rewrite
1 parent d77c61a commit fe59b45

File tree

17 files changed

+4158
-72
lines changed

17 files changed

+4158
-72
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
NODE_ENV=DEV
2+
PORT=5000

.eslintrc.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
'env': {
3+
'browser': true,
4+
'commonjs': true,
5+
'es2021': true,
6+
},
7+
'extends': [
8+
'google',
9+
],
10+
'parserOptions': {
11+
'ecmaVersion': 12,
12+
},
13+
'rules': {
14+
},
15+
};

.gitattributes

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Declare files that will always have LF line endings on checkout.
2+
3+
*.html text eol=lf
4+
*.scss text eol=lf
5+
*.sass text eol=lf
6+
*.css text eol=lf
7+
*.js text eol=lf
8+
*.json text eol=lf
9+
*.svg text eol=lf
10+
*.yml text eol=lf
11+
*.yaml text eol=lf
12+
*.md text eol=lf
13+
14+
.babelrc text eol=lf
15+
.gitignore text eol=lf
16+
.gitattributes text eol=lf
17+
18+
LICENSE text eol=lf
19+
20+
# Denote all files that are truly binary and should not be modified.
21+
22+
*.png binary
23+
*.jpg binary
24+
*.jpeg binary
25+
*.gif binary
26+
*.bmp binary
27+
*.ai binary
28+
*.psd binary
29+
*.pdf binary
30+
31+
*.otf binary
32+
*.eot binary
33+
*.ttf binary
34+
*.woff binary
35+
*.woff2 binary
36+
*.zip binary
37+
*.rar binary

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
# Change Log
22

3-
## Unreleased
4-
### Improvements
3+
## [0.0.3] 2021-07-04
4+
### Complete rewrite
55

6+
- Update Passport strategy to `JwtStrategy`
7+
- Persistance via MongoDB
8+
- API:
9+
- Sign UP: `/api/users/register`
10+
- Sign IN: `/api/users/login`
11+
- Logout: `/api/users/logout`
12+
- Check Session: `/api/users/checkSession`
13+
- Edit User: `/api/users/edit`
614
- Merge PR #5 - Added `nodemon` to `devDependencies`
715

816
## [0.0.2] 2021-07-03

README.md

Lines changed: 79 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,134 @@
11

22
# Nodejs API Server
33

4-
Express / Nodejs Starter with JWT authentication, SQLite database, Sequelize ORM, unit tests and basic tooling - Provided by **AppSeed** [App Generator](https://appseed.us/app-generator).
4+
Express / Nodejs Starter with JWT authentication, MongoDB - Provided by **AppSeed** [App Generator](https://appseed.us/app-generator).
5+
Authentication is based on [json web tokens](https://jwt.io). `passport-jwt` strategy is used to handle the Email/Password authentication. After a successful login the generated token is sent to the requester.
56

67
<br />
78

8-
> Status: **Work in progress** (complete rewrite).
9+
> Free Support:
10+
11+
- Github (issues tracker)
12+
- Email: *support @ appseed.us* (max 12h response time)
13+
- Discord: LIVE Support (registered AppSeed Users)
914

1015
<br />
1116

1217
## Requirements
1318

1419
- [Node.js](https://nodejs.org/) >= 10.x
20+
- [MongoDB](https://www.mongodb.com/) server
1521

1622
<br />
1723

18-
## Authentication
24+
## How to use the code
1925

20-
Authentication is based on [json web tokens](https://jwt.io). `passport-jwt` strategy is used to handle the Email/Password authentication. After a successful login the generated token is sent to the requester.
26+
**Clone the sources**
2127

22-
<br />
28+
```bash
29+
$ git clone https://github.com/app-generator/api-server-nodejs.git
30+
$ cd api-server-nodejs
31+
```
2332

24-
## API
33+
**Install dependencies** via NPM or Yarn
2534

26-
### Login: `api/users/login`
35+
```bash
36+
$ npm i
37+
// OR
38+
$ yarn
2739
```
28-
POST api/users/login
29-
Host: localhost:3000
30-
Content-Type: application/json
3140

32-
{
33-
"email": "demo@appseed.us",
34-
"password": "demo"
35-
}
36-
```
41+
**Start the API server**
3742

38-
### Signup: `/api/users/signup`
43+
```bash
44+
$ npm start
45+
// OR
46+
$ yarn start
3947
```
40-
POST api/users/signup
41-
Host: localhost:3000
42-
Content-Type: application/json
4348

44-
{
45-
"email": "demo@appseed.us",
46-
"password": "demo",
47-
"name": "George",
48-
"surname": "Clooney"
49-
}
50-
```
49+
The API server will start using the `PORT` specified in `.env` file (default 5000)
5150

5251
<br />
5352

54-
## Setting up for development
53+
## Codebase Structure
5554

56-
* clone repo: `git clone https://github.com/app-generator/nodejs-starter.git`
57-
* change directory to nodejs-starter:
58-
* create a file named .env which should contain the following default setup:
59-
60-
```
61-
SALT=35kj7waj3k5kja09jeoi21kn0pg13iuhlkn // used in password hashing
62-
JWT_SECRET=secret // used in JWT signing
63-
SESSION_SECRET=secret // used for session data
64-
PORT=3000 // the port on which your server will be available on
65-
SERVER_ADDRESS=127.0.0.1 // or 0.0.0.0 for all or other interface address you want to listen
55+
```bash
56+
< PROJECT ROOT >
57+
|
58+
|-- config/
59+
| |-- config.js # Configuration
60+
| |-- passport.js # Define Passport Startegy
61+
|
62+
|-- models/
63+
| |-- activeSession.js # Sessions Model (Mongo)
64+
| |-- user.js # User Model (Mongo)
65+
|
66+
|-- routes/
67+
| |-- users.js # Define Users API Routes
68+
|
69+
|
70+
|-- api.js # API Entry Point
71+
|-- .env # Specify the ENV variables
72+
|
73+
|-- ************************************************************************
6674
```
67-
* users are saved in file `config/users.js`
6875

6976
<br />
7077

71-
## Scripts
78+
## Mongo Settings
7279

73-
**Install Modules**
74-
```bash
75-
$ npm i
76-
$ npm i nodemon -g
80+
The Mongo URI lives in `config/keys.js`
81+
82+
```javascript
83+
... = 'mongodb://localhost/api_server_nodejs'
7784
```
7885

7986
<br />
8087

81-
**Run**
82-
```bash
83-
$ npm run start # classic start OR
84-
$ npm run dev # with nodemon live update
85-
```
86-
Runs the application with [nodemon]("https://nodemon.io/"). Server is listening on Port 3000 by default. This can be overwritten by `PORT` constant in `.env` file.
88+
## API
8789

88-
<br />
90+
For a fast set up, use this POSTMAN file: [api_sample](#)
8991

90-
## Curl tests
92+
> **Register** - `api/users/signup`
9193
92-
**Create user**
94+
```
95+
POST api/users/signup
96+
Content-Type: application/json
9397
94-
```bash
95-
$ curl -X POST -H 'Content-Type: application/json' \
96-
-d '{"username":"test1","password":"pass", "email":"test1@appseed.us"}' \
97-
http://localhost:3000/api/users/signup
98+
{
99+
"username":"test",
100+
"password":"pass",
101+
"email":"test@appseed.us"
102+
}
98103
```
99104

100105
<br />
101106

102-
**Login user**
107+
> **Login** - `api/users/login`
103108
104-
```bash
105-
$ curl -X POST -H 'Content-Type: application/json' \
106-
-d '{"email":"test1@appseed.us", "password":"pass"}' \
107-
http://localhost:3000/api/users/login
108109
```
110+
POST /api/users/login
111+
Content-Type: application/json
109112
110-
<br />
111-
112-
**List Users**
113-
114-
```bash
115-
$ curl -H 'Content-Type: application/json' http://localhost:3000/api/users/list
113+
{
114+
"password":"pass",
115+
"email":"test@appseed.us"
116+
}
116117
```
117118

118119
<br />
119120

120-
## Support
121+
> **Logout** - `api/users/logout`
121122
122-
For issues and features request, use **Github** or access the [support page](https://appseed.us/support) provided by **AppSeed**
123+
```
124+
POST api/users/logout
125+
Content-Type: application/json
126+
authorization: JWT_TOKEN (returned by Login request)
127+
128+
{
129+
"token":"JWT_TOKEN"
130+
}
131+
```
123132

124133
<br />
125134

@@ -130,4 +139,4 @@ MIT @ [AppSeed](https://appseed.us)
130139
<br />
131140

132141
---
133-
[Nodejs Starter](https://appseed.us/boilerplate-code/nodejs-starter) provided by **AppSeed**
142+
[Nodejs Starter](https://appseed.us/boilerplate-code/nodejs-starter) - provided by AppSeed [App Generator](https://appseed.us)

api.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* eslint-disable max-len */
2+
/*
3+
4+
Copyright (c) 2019 - present AppSeed.us
5+
6+
*/
7+
const express = require('express');
8+
const bodyParser = require('body-parser');
9+
const passport = require('passport');
10+
const mongoose = require('mongoose');
11+
const compression = require('compression');
12+
const https = require('https');
13+
const http = require('http');
14+
const fs = require('fs');
15+
const cors = require('cors');
16+
const path = require('path');
17+
const db = require('./config/keys').mongoURI;
18+
19+
require('dotenv').config();
20+
21+
// Instantiate express
22+
const app = express();
23+
app.use(compression());
24+
25+
// Passport Config
26+
require('./config/passport')(passport);
27+
28+
// DB Config
29+
30+
// Connect to MongoDB
31+
mongoose
32+
.connect(
33+
db, {useNewUrlParser: true,
34+
useFindAndModify: false,
35+
useUnifiedTopology: true,
36+
useCreateIndex: true},
37+
)
38+
.then(() => console.log('MongoDB Connected'))
39+
.catch((err) => console.log(err));
40+
41+
app.use(cors());
42+
43+
// Express body parser
44+
app.use('/public', express.static('public'));
45+
app.use(bodyParser.urlencoded({extended: true}));
46+
app.use(bodyParser.json());
47+
48+
// Initialize routes middleware
49+
app.use('/api/users', require('./routes/users'));
50+
51+
const PORT = process.env.PORT;
52+
53+
http.createServer({
54+
}, app)
55+
.listen(PORT, function() {
56+
console.log('App listening on port ' + PORT + '! Go to http://localhost:' + PORT + '/');
57+
});
58+

config/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable max-len */
2+
/*
3+
4+
Copyright (c) 2019 - present AppSeed.us
5+
6+
*/
7+
module.exports = {
8+
webURL: 'https://localhost:5000/',
9+
};

config/keys.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-disable max-len */
2+
/*
3+
4+
Copyright (c) 2019 - present AppSeed.us
5+
6+
*/
7+
dbPasswordDev = 'mongodb://localhost/api_server_nodejs';
8+
9+
module.exports = {
10+
mongoURI: dbPasswordDev,
11+
secret: 'SuperS3cret_4277m',
12+
};

config/passport.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-disable max-len */
2+
/*
3+
4+
Copyright (c) 2019 - present AppSeed.us
5+
6+
*/
7+
const JwtStrategy = require('passport-jwt').Strategy;
8+
const ExtractJwt = require('passport-jwt').ExtractJwt;
9+
const User = require('../models/user');
10+
const config = require('./keys');
11+
12+
module.exports = function(passport) {
13+
const opts = {};
14+
opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
15+
opts.secretOrKey = config.secret;
16+
17+
passport.use(new JwtStrategy(opts, (jwtPayload, done) => {
18+
User.findById(jwtPayload._doc._id, (err, user) => {
19+
if (err) {
20+
return done(err, false);
21+
}
22+
if (user) {
23+
return done(null, user);
24+
} else {
25+
return done(null, false);
26+
}
27+
});
28+
}));
29+
};

0 commit comments

Comments
 (0)