npx gitignore node
- Add Mac Cruft inside gitignore
- Add package.json
npm init -y
- Install nodemon, jest and supertest as development dependencies
npm i -D nodemon jest supertest
npm install knex
npm install express
npm install -g knex
npm i bcryptjs
npm i cors
npm i helmet
npm install jsonwebtoken
npm i dotenv
npm i pg
- Add scripts
"test": "jest --watch"
"server": "nodemon index.js"
"start": "node index.js"
"jest": {
"testEnvironment": "node"
}
- .env
- index.js
- api
api-router.js
configure-middleware.js
server.js
- auth
auth-router.js
restricted-middleware.js
- data
dbConfig.js
- users
users-model.js
users-router.js
- config
secrets.js
- Add
PORT=5000
- Add
require('dotenv').config();
at the very top - Add
const server = require('./api/server.js');
- Add
const PORT = process.env.PORT || 5000;
- Add
server.listen(PORT, () => console.log(
Listening on port ${PORT}...));
- Add
const express = require('express');
- Add
const router = express.Router();
- Add
const Users = require('./users-model');
- Add
module.exports = router;
// Need to change
- Add
const knex = require('knex');
- Add
const configOptions = require('../knexfile').development;
- Add
module.exports = knex(configOptions);
- Add
const db = require('../data/dbConfig');
- Add
module.exports = {}
- Add
const express = require('express');
- Add
const apiRouter = require('./api-router.js');
- Add
const configureMiddleware = require('./configure-middleware.js');
- Add
const server = express();
- Add
configureMiddleware(server);
- Add
server.use('/api', apiRouter);
- Add (optional)
server.get('/', (req, res) => res.send('<h1>Hello from Node auth1 Project</h1>'));
- Add
module.exports = server;
- Add:
const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
module.exports = server => {
server.use(helmet());
server.use(express.json());
server.use(cors());
};
- Add
const router = require('express').Router();
- Add
const authRouter = require('../auth/auth-router.js');
- Add
const usersRouter = require('../users/users-router.js');
- Add
const restrcited = require('../auth/restricted-middleware');
- Add
router.use('/auth', authRouter);
- Add
router.use('/users', restrcited, usersRouter);
- Add
module.exports = router;
- Add
const bcrypt = require('bcryptjs');
- Add
const router = require('express').Router();
- Add
const Users = require('../users/users-model.js');
- Add
module.exports = router;
- Add
const secrets = require('../config/secrets');
- Add JWT_SECRET=I like pineapple on pizza
- Add
const jwt = require('jsonwebtoken');
- Add:
module.exports = {
jwtSecret: process.env.JWT_SECRET || 'I like pineapple on pizza',
generateToken(user) {
const payload = {
subject: user.id,
username: user.username,
department: user.department
}
const options = {
expiresIn: '1h'
}
return jwt.sign(payload, this.jwtSecret, options)
}
}
- Add functions for
- get
- getBy
- getById
- add
- Add get request
- Add
module.exports = (req, res, next) => etc
- See
restricted-middleware.js
for details
- issues
issues-router.js
issues-model.js
- add endpoints and models
- states
states-router.js
states-model.js
- add endpoints and models
knex init
BEFORE
module.exports = {
development: {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3'
}
},
}
knex migrate:make create_users_table
-
Create a table however you please
-
Add migration
knex migrate:latest
-----TO DO-----
- Run
npm install knex-cleaner
- Run
knex seed:make 00-cleanup
- Inside the cleanup seed add
const cleaner = require('knex-cleaner');
exports.seed = function(knex) {
return cleaner.clean(knex, {
// resets ids
mode: 'delete',
// don't empty migration tables
ignoreTables: ['knex_migrations', 'knex_migrations_lock'],
});
};
- This removes all tables (excluding the two tables that track migrations) in the correct order before any seed files run.
- Important create seeds in the same order you created your tables
- In other words, don’t create a seed with a foreign key, until that reference record exists Example Seeds
knex seed:make 01-users
knex seed:make 02-profile
- Change all
table_name
's to the name of the table - Remove the following code block:
return knex('profile').del()
.then(function () {
});
- Seeds should now look like:
exports.seed = function(knex) {
// Inserts seed entries
return knex('profile').insert([
{id: 1, colName: 'rowValue1'},
{id: 2, colName: 'rowValue2'},
{id: 3, colName: 'rowValue3'}
]);
};
- Add data to your seeds accordingly
- Run
knex seed:run
when done