Skip to content

Commit

Permalink
start repo with blank files
Browse files Browse the repository at this point in the history
  • Loading branch information
grobergm committed Aug 3, 2019
1 parent a276b83 commit 48497d5
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 81 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ This is a reference for the NodeJS and Express intro tutorial. Using this, you c
## Setup

1. Download or clone this repo
2. Install Depandancies: npm install
3. Navigate to the root directory
2. Navigate to the root directory
3. Install Depandancies: npm install
4. Start the server file: node server.js
18 changes: 0 additions & 18 deletions middleware.js
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
const checkToken=function(req,res,next){
let token=req.headers['authorization']

if (token){
// In practice use json web token verify method to check if token is valid
// when next is called in middleware, it will run the next function
next()
} else {
res.json({
success:false,
message:'no token in headers'
})
}
}

module.exports= {
checkToken:checkToken
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
60 changes: 0 additions & 60 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,60 +0,0 @@
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const middleware = require('./middleware.js');

app.use(bodyParser.json())

const mockUserData=[
{name:'Mark'},
{name:'Jill'}
]

app.get('/users',function(req,res){
res.json({
success: true,
message: 'successfully got users. Nice!',
users: mockUserData
})
})
// colons are used as variables that be viewed in the params
app.get('/users/:id',function(req,res){
console.log(req.params.id)
})

app.post('/login',function(req,res){
// Typically passwords are encrypted using something like bcrypt before sending to database
const username=req.body.username;
const password=req.body.password;

// This should come from the database
const mockUsername="billyTheKid";
const mockPassword="superSecret";

if (username===mockUsername && password===mockPassword){
// In practice, use JSON web token sign method here to make an encrypted token
res.json({
success: true,
message: 'password and username match!',
token: 'encrypted token goes here'
})
} else {
res.json({
success: false,
message: 'password and username do not match'
})
}

})
// admin route is protected by checking token in middleware
app.get('/admin',middleware.checkToken,function(req,res){

res.json({
success:true,
message:'admin authorized',
adminData: 'secure data from database'
})

})

app.listen(8000,function(){console.log('server is listening')})

0 comments on commit 48497d5

Please sign in to comment.