Skip to content

Commit c11c8df

Browse files
committed
server and database setup complete
1 parent 60645d3 commit c11c8df

24 files changed

+3337
-1
lines changed

.DS_Store

8 KB
Binary file not shown.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# blog
1+
# project-2

config/connection.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Sequelize = require("sequelize");
2+
require("dotenv").config();
3+
4+
let sequelize;
5+
6+
if (process.env.JAWSDB_URL) {
7+
sequelize = new Sequelize(process.env.JAWSDB_URL);
8+
} else {
9+
sequelize = new Sequelize(
10+
process.env.DB_NAME,
11+
process.env.DB_USER,
12+
process.env.DB_PASSWORD,
13+
{
14+
host: "localhost",
15+
dialect: "mysql",
16+
port: 3306,
17+
}
18+
);
19+
}
20+
21+
module.exports = sequelize;

controllers/api/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Routes for /api/whatever
2+
3+
const router = require('express').Router();
4+
5+
6+
// router.use('/', homeRoutes);
7+
8+
9+
module.exports = router;

controllers/homeRoutes.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Routes for /whatever
2+
3+
const router = require('express').Router();
4+
5+
6+
// router.use('/', homeRoutes);
7+
8+
9+
module.exports = router;

controllers/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const router = require('express').Router();
2+
3+
const apiRoutes = require('./api');
4+
const homeRoutes = require('./homeRoutes');
5+
6+
router.use('/', homeRoutes);
7+
router.use('/api', apiRoutes);
8+
9+
module.exports = router;

db/schema.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DROP DATABASE IF EXISTS colab_db;
2+
CREATE DATABASE colab_db;
3+
4+
USE colab_db;
5+

helpers/fsUtils.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const fs = require('fs');
2+
const util = require('util');
3+
4+
// Promise version of fs.readFile
5+
const readFromFile = util.promisify(fs.readFile);
6+
/**
7+
* Function to write data to the JSON file given a destination and some content
8+
* @param {string} destination The file you want to write to.
9+
* @param {object} content The content you want to write to the file.
10+
* @returns {void} Nothing
11+
*/
12+
const writeToFile = (destination,content) =>
13+
fs.writeFile(destination,JSON.stringify(content,null,4),(err) =>
14+
err ? console.error(err) : console.info(`\nData written to ${destination}`)
15+
);
16+
/**
17+
* Function to read data from a given a file and append some content
18+
* @param {object} content The content you want to append to the file.
19+
* @param {string} file The path to the file you want to save to.
20+
* @returns {void} Nothing
21+
*/
22+
const readAndAppend = (content,file) =>
23+
{
24+
fs.readFile(file,'utf8',(err,data) =>
25+
{
26+
if (err) {
27+
console.error(err);
28+
} else {
29+
const parsedData = JSON.parse(data);
30+
parsedData.push(content);
31+
writeToFile(file,parsedData);
32+
}
33+
});
34+
};
35+
36+
module.exports = { readFromFile,writeToFile,readAndAppend };

helpers/uuid.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Immediately export a function that generates a string of random numbers and letters
2+
module.exports = () =>
3+
Math.floor((1 + Math.random()) * 0x10000)
4+
.toString(16)
5+
.substring(1);

middleware/clog.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Custom middleware that logs out the type and path of each request to the server
2+
const clog = (req, res, next) => {
3+
const fgCyan = '\x1b[36m';
4+
switch (req.method) {
5+
case 'GET': {
6+
console.info(`📗 ${fgCyan}${req.method} request to ${req.path}`);
7+
break;
8+
}
9+
case 'POST': {
10+
console.info(`📘 ${fgCyan}${req.method} request to ${req.path}`);
11+
break;
12+
}
13+
default:
14+
console.log(`📙${fgCyan}${req.method} request to ${req.path}`);
15+
}
16+
17+
18+
next();
19+
};
20+
21+
exports.clog = clog;

models/User.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const { Model, DataTypes } = require('sequelize');
2+
const bcrypt = require('bcrypt');
3+
const sequelize = require('../config/connection');
4+
5+
class User extends Model {
6+
checkPassword(loginPw) {
7+
return bcrypt.compareSync(loginPw, this.password);
8+
}
9+
}
10+
11+
User.init(
12+
{
13+
id: {
14+
type: DataTypes.INTEGER,
15+
allowNull: false,
16+
primaryKey: true,
17+
autoIncrement: true,
18+
},
19+
username: {
20+
type: DataTypes.STRING,
21+
},
22+
email: {
23+
type: DataTypes.STRING,
24+
allowNull: false,
25+
unique: true,
26+
validate: {
27+
isEmail: true,
28+
},
29+
},
30+
password: {
31+
type: DataTypes.STRING,
32+
allowNull: false,
33+
validate: {
34+
len: [8],
35+
},
36+
},
37+
},
38+
{
39+
hooks: {
40+
beforeCreate: async (newUserData) => {
41+
newUserData.password = await bcrypt.hash(newUserData.password, 10);
42+
return newUserData;
43+
},
44+
beforeUpdate: async (updatedUserData) => {
45+
updatedUserData.password = await bcrypt.hash(updatedUserData.password, 10);
46+
return updatedUserData;
47+
},
48+
},
49+
sequelize,
50+
modelName: 'user',
51+
}
52+
);
53+
54+
module.exports = User;

models/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// ADD RELATIONSHIPS HERE
2+
3+
const User = require('./User');
4+
5+
module.exports = {
6+
User
7+
8+
}

0 commit comments

Comments
 (0)