all days work done
This commit is contained in:
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
# node_task_2
|
# day 14
|
||||||
|
|
||||||
1. Clone repo to your own github
|
1. Clone repo to your own github
|
||||||
2. Read the configuration.json
|
2. Read the configuration.json
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
# day 15
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
- setup project
|
||||||
|
- clone to your github
|
||||||
|
- Open figma file https://www.figma.com/file/Zr1EutaLsykPcADfC3el7r/ai-marketing-site-sample?node-id=0%3A1
|
||||||
|
- Need you to finish the site by end of day both mobile and desktop responsive
|
||||||
|
- Use bootstrap 4
|
||||||
|
- CSS should be clean, not using inline style CSS but proper classes
|
||||||
|
- deploy site using github pages https://www.codecademy.com/articles/f1-u3-github-pages
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
# day 16
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
- setup project
|
||||||
|
- clone to your github
|
||||||
|
|
||||||
|
1. Read http://fabricjs.com/articles/
|
||||||
|
2. Look at Background page image. Make this page using fabricjs. We dont need icon above the menu names. Choose 10 random default colors. If color clicked, the canvas color changes to it. Clicking on download button down the canvas.
|
||||||
|
3. Look at text page image. Clicking on text menu open the left menu to add an h1 tag, h6 tag or paragraph tag. Clicking one of these add the default text in center of canvas. Clicking on default text can edit. You can move the text around. The 60 dropdown is to set the font size. The trash icon is to delete the text
|
||||||
|
4. Look at image page image. You will call https://picsum.photos/v2/list with ajax to load images on left panel. Clicking on an image in left panel put the image in center of canvas. Clicking on trash icon will delete the image
|
||||||
|
5. Need to be done by end of day
|
||||||
|
6. Deploy code to github pages so Client can see it
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 219 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.5 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 322 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 265 KiB |
@@ -0,0 +1,8 @@
|
|||||||
|
# day 17
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
- setup project
|
||||||
|
- clone to your github
|
||||||
|
- Make the frontend for this project https://www.figma.com/file/iaKhmTAN28YiYXAOZAr9rN/Scheduler-Task?node-id=0%3A1
|
||||||
|
- timezones are generated dynamically
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
var createError = require('http-errors');
|
||||||
|
var express = require('express');
|
||||||
|
var path = require('path');
|
||||||
|
var cookieParser = require('cookie-parser');
|
||||||
|
var logger = require('morgan');
|
||||||
|
|
||||||
|
var indexRouter = require('./routes/index');
|
||||||
|
var usersRouter = require('./routes/users');
|
||||||
|
|
||||||
|
const db = require("./models");
|
||||||
|
var cors = require("cors");
|
||||||
|
|
||||||
|
var app = express();
|
||||||
|
app.set("db", db);
|
||||||
|
// view engine setup
|
||||||
|
app.set('views', path.join(__dirname, 'views'));
|
||||||
|
app.set('view engine', 'jade');
|
||||||
|
app.use(cors());
|
||||||
|
app.use(logger('dev'));
|
||||||
|
app.use(express.json());
|
||||||
|
app.use(express.urlencoded({ extended: false }));
|
||||||
|
app.use(cookieParser());
|
||||||
|
app.use(express.static(path.join(__dirname, 'public')));
|
||||||
|
|
||||||
|
app.use('/', indexRouter);
|
||||||
|
app.use('/users', usersRouter);
|
||||||
|
|
||||||
|
// catch 404 and forward to error handler
|
||||||
|
app.use(function (req, res, next) {
|
||||||
|
next(createError(404));
|
||||||
|
});
|
||||||
|
|
||||||
|
// error handler
|
||||||
|
app.use(function (err, req, res, next) {
|
||||||
|
// set locals, only providing error in development
|
||||||
|
res.locals.message = err.message;
|
||||||
|
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
||||||
|
|
||||||
|
// render the error page
|
||||||
|
res.status(err.status || 500);
|
||||||
|
res.render('error');
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = app;
|
||||||
Executable
+90
@@ -0,0 +1,90 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var app = require('../app');
|
||||||
|
var debug = require('debug')('day-1:server');
|
||||||
|
var http = require('http');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get port from environment and store in Express.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var port = normalizePort(process.env.PORT || '3000');
|
||||||
|
app.set('port', port);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create HTTP server.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var server = http.createServer(app);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen on provided port, on all network interfaces.
|
||||||
|
*/
|
||||||
|
|
||||||
|
server.listen(port);
|
||||||
|
server.on('error', onError);
|
||||||
|
server.on('listening', onListening);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize a port into a number, string, or false.
|
||||||
|
*/
|
||||||
|
|
||||||
|
function normalizePort(val) {
|
||||||
|
var port = parseInt(val, 10);
|
||||||
|
|
||||||
|
if (isNaN(port)) {
|
||||||
|
// named pipe
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (port >= 0) {
|
||||||
|
// port number
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event listener for HTTP server "error" event.
|
||||||
|
*/
|
||||||
|
|
||||||
|
function onError(error) {
|
||||||
|
if (error.syscall !== 'listen') {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
var bind = typeof port === 'string'
|
||||||
|
? 'Pipe ' + port
|
||||||
|
: 'Port ' + port;
|
||||||
|
|
||||||
|
// handle specific listen errors with friendly messages
|
||||||
|
switch (error.code) {
|
||||||
|
case 'EACCES':
|
||||||
|
console.error(bind + ' requires elevated privileges');
|
||||||
|
process.exit(1);
|
||||||
|
break;
|
||||||
|
case 'EADDRINUSE':
|
||||||
|
console.error(bind + ' is already in use');
|
||||||
|
process.exit(1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event listener for HTTP server "listening" event.
|
||||||
|
*/
|
||||||
|
|
||||||
|
function onListening() {
|
||||||
|
var addr = server.address();
|
||||||
|
var bind = typeof addr === 'string'
|
||||||
|
? 'pipe ' + addr
|
||||||
|
: 'port ' + addr.port;
|
||||||
|
debug('Listening on ' + bind);
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
'use strict';
|
||||||
|
/*Powered By: Manaknightdigital Inc. https://manaknightdigital.com/ Year: 2020*/
|
||||||
|
/**
|
||||||
|
* Sequelize File
|
||||||
|
* @copyright 2020 Manaknightdigital Inc.
|
||||||
|
* @link https://manaknightdigital.com
|
||||||
|
* @license Proprietary Software licensing
|
||||||
|
* @author Ryan Wong
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
let Sequelize = require('sequelize');
|
||||||
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
|
const config = {
|
||||||
|
DB_DATABASE: 'mysql',
|
||||||
|
DB_USERNAME: 'root',
|
||||||
|
DB_PASSWORD: 'root',
|
||||||
|
DB_ADAPTER: 'mysql',
|
||||||
|
DB_NAME: 'day_1',
|
||||||
|
DB_HOSTNAME: 'localhost',
|
||||||
|
DB_PORT: 3306,
|
||||||
|
};
|
||||||
|
|
||||||
|
let db = {};
|
||||||
|
|
||||||
|
let sequelize = new Sequelize(config.DB_DATABASE, config.DB_USERNAME, config.DB_PASSWORD, {
|
||||||
|
dialect: config.DB_ADAPTER,
|
||||||
|
username: config.DB_USERNAME,
|
||||||
|
password: config.DB_PASSWORD,
|
||||||
|
database: config.DB_NAME,
|
||||||
|
host: config.DB_HOSTNAME,
|
||||||
|
port: config.DB_PORT,
|
||||||
|
logging: console.log,
|
||||||
|
timezone: '-04:00',
|
||||||
|
pool: {
|
||||||
|
maxConnections: 1,
|
||||||
|
minConnections: 0,
|
||||||
|
maxIdleTime: 100,
|
||||||
|
},
|
||||||
|
define: {
|
||||||
|
timestamps: false,
|
||||||
|
underscoredAll: true,
|
||||||
|
underscored: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// sequelize.sync({ force: true });
|
||||||
|
|
||||||
|
fs.readdirSync(__dirname)
|
||||||
|
.filter((file) => {
|
||||||
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
|
})
|
||||||
|
.forEach((file) => {
|
||||||
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
|
db[model.name] = model;
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.keys(db).forEach((modelName) => {
|
||||||
|
if (db[modelName].associate) {
|
||||||
|
db[modelName].associate(db);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
db.sequelize = sequelize;
|
||||||
|
db.Sequelize = Sequelize;
|
||||||
|
|
||||||
|
module.exports = db;
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
module.exports = (sequelize, DataTypes) => {
|
||||||
|
const location = sequelize.define(
|
||||||
|
"location",
|
||||||
|
{
|
||||||
|
id: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
primaryKey: true,
|
||||||
|
autoIncrement: true,
|
||||||
|
},
|
||||||
|
name: DataTypes.STRING,
|
||||||
|
created_at: DataTypes.DATEONLY,
|
||||||
|
updated_at: DataTypes.DATE,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
freezeTableName: true,
|
||||||
|
tableName: "location",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
underscoredAll: false,
|
||||||
|
underscored: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return location;
|
||||||
|
};
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "day-1",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"start": "node ./bin/www"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"cookie-parser": "~1.4.4",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"debug": "~2.6.9",
|
||||||
|
"express": "~4.16.1",
|
||||||
|
"http-errors": "~1.6.3",
|
||||||
|
"jade": "~1.11.0",
|
||||||
|
"morgan": "~1.9.1",
|
||||||
|
"mysql2": "^2.3.3",
|
||||||
|
"sequelize": "^6.15.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
body {
|
||||||
|
padding: 50px;
|
||||||
|
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: #00B7FF;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router();
|
||||||
|
|
||||||
|
/* GET home page. */
|
||||||
|
router.get('/', function(req, res, next) {
|
||||||
|
res.render('index', { title: 'Express' });
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router();
|
||||||
|
|
||||||
|
/* GET users listing. */
|
||||||
|
router.get('/', function(req, res, next) {
|
||||||
|
res.send('respond with a resource');
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
const { Validator, addCustomMessages } = require('node-input-validator');
|
||||||
|
|
||||||
|
// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"}
|
||||||
|
const formatValidationError = (error) => {
|
||||||
|
const formatted = Object.entries(error)
|
||||||
|
.map(([key, value]) => ({
|
||||||
|
field: key,
|
||||||
|
message: value.message,
|
||||||
|
}))
|
||||||
|
.reduce((accumulator, currentValue) => {
|
||||||
|
if (!accumulator[currentValue]) {
|
||||||
|
accumulator[currentValue.field] = currentValue.message;
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}, {});
|
||||||
|
return formatted;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
/**
|
||||||
|
* Input Validator middleware for controller
|
||||||
|
* @param {object} validationObject object defining body fields and its validation types eg:{email:required|email}
|
||||||
|
* @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"}
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
validateInput: (validationObject = {}, _extendMessages = {}) => async (
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
next,
|
||||||
|
) => {
|
||||||
|
const validation = new Validator(req.body, validationObject);
|
||||||
|
addCustomMessages(_extendMessages);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const isValid = await validation.check();
|
||||||
|
if (!isValid) {
|
||||||
|
req.validationError = formatValidationError(validation.errors);
|
||||||
|
}
|
||||||
|
return next();
|
||||||
|
} catch (error) {
|
||||||
|
req.validationError = error.message;
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
handleValidationErrorForViews: (
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
viewModel,
|
||||||
|
viewPath = '/',
|
||||||
|
fieldsStoreKey,
|
||||||
|
defaultValue = {},
|
||||||
|
) => {
|
||||||
|
const validationError = req.validationError;
|
||||||
|
|
||||||
|
if (validationError) {
|
||||||
|
// Remembers fields if validation error occurs
|
||||||
|
Object.entries(defaultValue).forEach(([key, value]) => {
|
||||||
|
viewModel[fieldsStoreKey][key] = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (typeof validationError === 'string') {
|
||||||
|
viewModel.error = validationError;
|
||||||
|
} else {
|
||||||
|
viewModel.validationError = req.validationError;
|
||||||
|
}
|
||||||
|
return res.render(viewPath, viewModel);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
handleValidationErrorForAPI: (req, res, next) => {
|
||||||
|
const validationError = req.validationError;
|
||||||
|
|
||||||
|
if (validationError) {
|
||||||
|
let error;
|
||||||
|
if (typeof validationError === 'string') {
|
||||||
|
error = validationError;
|
||||||
|
} else {
|
||||||
|
error = req.validationError;
|
||||||
|
}
|
||||||
|
return res.json({ success: false, error });
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
extends layout
|
||||||
|
|
||||||
|
block content
|
||||||
|
h1= message
|
||||||
|
h2= error.status
|
||||||
|
pre #{error.stack}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
extends layout
|
||||||
|
|
||||||
|
block content
|
||||||
|
h1= title
|
||||||
|
p Welcome to #{title}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
doctype html
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title= title
|
||||||
|
link(rel='stylesheet', href='/stylesheets/style.css')
|
||||||
|
body
|
||||||
|
block content
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# day 18
|
||||||
|
|
||||||
|
Continue day 17 if not done
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# day 18
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
- setup project
|
||||||
|
- clone to your github
|
||||||
|
- create table schedule(times booked off by clients), availability (hours user is available)
|
||||||
|
- Make the api /api/v1/schedule that save the appointment to the database table schedule
|
||||||
|
- Make api /api/v1/booked that return available times excluding schedule. Use this api to show the times in calendar
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
var createError = require('http-errors');
|
||||||
|
var express = require('express');
|
||||||
|
var path = require('path');
|
||||||
|
var cookieParser = require('cookie-parser');
|
||||||
|
var logger = require('morgan');
|
||||||
|
|
||||||
|
var indexRouter = require('./routes/index');
|
||||||
|
var usersRouter = require('./routes/users');
|
||||||
|
|
||||||
|
const db = require("./models");
|
||||||
|
var cors = require("cors");
|
||||||
|
|
||||||
|
var app = express();
|
||||||
|
app.set("db", db);
|
||||||
|
// view engine setup
|
||||||
|
app.set('views', path.join(__dirname, 'views'));
|
||||||
|
app.set('view engine', 'jade');
|
||||||
|
app.use(cors());
|
||||||
|
app.use(logger('dev'));
|
||||||
|
app.use(express.json());
|
||||||
|
app.use(express.urlencoded({ extended: false }));
|
||||||
|
app.use(cookieParser());
|
||||||
|
app.use(express.static(path.join(__dirname, 'public')));
|
||||||
|
|
||||||
|
app.use('/', indexRouter);
|
||||||
|
app.use('/users', usersRouter);
|
||||||
|
|
||||||
|
// catch 404 and forward to error handler
|
||||||
|
app.use(function (req, res, next) {
|
||||||
|
next(createError(404));
|
||||||
|
});
|
||||||
|
|
||||||
|
// error handler
|
||||||
|
app.use(function (err, req, res, next) {
|
||||||
|
// set locals, only providing error in development
|
||||||
|
res.locals.message = err.message;
|
||||||
|
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
||||||
|
|
||||||
|
// render the error page
|
||||||
|
res.status(err.status || 500);
|
||||||
|
res.render('error');
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = app;
|
||||||
Executable
+90
@@ -0,0 +1,90 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var app = require('../app');
|
||||||
|
var debug = require('debug')('day-1:server');
|
||||||
|
var http = require('http');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get port from environment and store in Express.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var port = normalizePort(process.env.PORT || '3000');
|
||||||
|
app.set('port', port);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create HTTP server.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var server = http.createServer(app);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen on provided port, on all network interfaces.
|
||||||
|
*/
|
||||||
|
|
||||||
|
server.listen(port);
|
||||||
|
server.on('error', onError);
|
||||||
|
server.on('listening', onListening);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize a port into a number, string, or false.
|
||||||
|
*/
|
||||||
|
|
||||||
|
function normalizePort(val) {
|
||||||
|
var port = parseInt(val, 10);
|
||||||
|
|
||||||
|
if (isNaN(port)) {
|
||||||
|
// named pipe
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (port >= 0) {
|
||||||
|
// port number
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event listener for HTTP server "error" event.
|
||||||
|
*/
|
||||||
|
|
||||||
|
function onError(error) {
|
||||||
|
if (error.syscall !== 'listen') {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
var bind = typeof port === 'string'
|
||||||
|
? 'Pipe ' + port
|
||||||
|
: 'Port ' + port;
|
||||||
|
|
||||||
|
// handle specific listen errors with friendly messages
|
||||||
|
switch (error.code) {
|
||||||
|
case 'EACCES':
|
||||||
|
console.error(bind + ' requires elevated privileges');
|
||||||
|
process.exit(1);
|
||||||
|
break;
|
||||||
|
case 'EADDRINUSE':
|
||||||
|
console.error(bind + ' is already in use');
|
||||||
|
process.exit(1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event listener for HTTP server "listening" event.
|
||||||
|
*/
|
||||||
|
|
||||||
|
function onListening() {
|
||||||
|
var addr = server.address();
|
||||||
|
var bind = typeof addr === 'string'
|
||||||
|
? 'pipe ' + addr
|
||||||
|
: 'port ' + addr.port;
|
||||||
|
debug('Listening on ' + bind);
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
'use strict';
|
||||||
|
/*Powered By: Manaknightdigital Inc. https://manaknightdigital.com/ Year: 2020*/
|
||||||
|
/**
|
||||||
|
* Sequelize File
|
||||||
|
* @copyright 2020 Manaknightdigital Inc.
|
||||||
|
* @link https://manaknightdigital.com
|
||||||
|
* @license Proprietary Software licensing
|
||||||
|
* @author Ryan Wong
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
let Sequelize = require('sequelize');
|
||||||
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
|
const config = {
|
||||||
|
DB_DATABASE: 'mysql',
|
||||||
|
DB_USERNAME: 'root',
|
||||||
|
DB_PASSWORD: 'root',
|
||||||
|
DB_ADAPTER: 'mysql',
|
||||||
|
DB_NAME: 'day_1',
|
||||||
|
DB_HOSTNAME: 'localhost',
|
||||||
|
DB_PORT: 3306,
|
||||||
|
};
|
||||||
|
|
||||||
|
let db = {};
|
||||||
|
|
||||||
|
let sequelize = new Sequelize(config.DB_DATABASE, config.DB_USERNAME, config.DB_PASSWORD, {
|
||||||
|
dialect: config.DB_ADAPTER,
|
||||||
|
username: config.DB_USERNAME,
|
||||||
|
password: config.DB_PASSWORD,
|
||||||
|
database: config.DB_NAME,
|
||||||
|
host: config.DB_HOSTNAME,
|
||||||
|
port: config.DB_PORT,
|
||||||
|
logging: console.log,
|
||||||
|
timezone: '-04:00',
|
||||||
|
pool: {
|
||||||
|
maxConnections: 1,
|
||||||
|
minConnections: 0,
|
||||||
|
maxIdleTime: 100,
|
||||||
|
},
|
||||||
|
define: {
|
||||||
|
timestamps: false,
|
||||||
|
underscoredAll: true,
|
||||||
|
underscored: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// sequelize.sync({ force: true });
|
||||||
|
|
||||||
|
fs.readdirSync(__dirname)
|
||||||
|
.filter((file) => {
|
||||||
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
|
})
|
||||||
|
.forEach((file) => {
|
||||||
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
|
db[model.name] = model;
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.keys(db).forEach((modelName) => {
|
||||||
|
if (db[modelName].associate) {
|
||||||
|
db[modelName].associate(db);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
db.sequelize = sequelize;
|
||||||
|
db.Sequelize = Sequelize;
|
||||||
|
|
||||||
|
module.exports = db;
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
module.exports = (sequelize, DataTypes) => {
|
||||||
|
const location = sequelize.define(
|
||||||
|
"location",
|
||||||
|
{
|
||||||
|
id: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
primaryKey: true,
|
||||||
|
autoIncrement: true,
|
||||||
|
},
|
||||||
|
name: DataTypes.STRING,
|
||||||
|
created_at: DataTypes.DATEONLY,
|
||||||
|
updated_at: DataTypes.DATE,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
freezeTableName: true,
|
||||||
|
tableName: "location",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
underscoredAll: false,
|
||||||
|
underscored: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return location;
|
||||||
|
};
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "day-1",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"start": "node ./bin/www"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"cookie-parser": "~1.4.4",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"debug": "~2.6.9",
|
||||||
|
"express": "~4.16.1",
|
||||||
|
"http-errors": "~1.6.3",
|
||||||
|
"jade": "~1.11.0",
|
||||||
|
"morgan": "~1.9.1",
|
||||||
|
"mysql2": "^2.3.3",
|
||||||
|
"sequelize": "^6.15.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
body {
|
||||||
|
padding: 50px;
|
||||||
|
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: #00B7FF;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router();
|
||||||
|
|
||||||
|
/* GET home page. */
|
||||||
|
router.get('/', function(req, res, next) {
|
||||||
|
res.render('index', { title: 'Express' });
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router();
|
||||||
|
|
||||||
|
/* GET users listing. */
|
||||||
|
router.get('/', function(req, res, next) {
|
||||||
|
res.send('respond with a resource');
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
const { Validator, addCustomMessages } = require('node-input-validator');
|
||||||
|
|
||||||
|
// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"}
|
||||||
|
const formatValidationError = (error) => {
|
||||||
|
const formatted = Object.entries(error)
|
||||||
|
.map(([key, value]) => ({
|
||||||
|
field: key,
|
||||||
|
message: value.message,
|
||||||
|
}))
|
||||||
|
.reduce((accumulator, currentValue) => {
|
||||||
|
if (!accumulator[currentValue]) {
|
||||||
|
accumulator[currentValue.field] = currentValue.message;
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}, {});
|
||||||
|
return formatted;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
/**
|
||||||
|
* Input Validator middleware for controller
|
||||||
|
* @param {object} validationObject object defining body fields and its validation types eg:{email:required|email}
|
||||||
|
* @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"}
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
validateInput: (validationObject = {}, _extendMessages = {}) => async (
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
next,
|
||||||
|
) => {
|
||||||
|
const validation = new Validator(req.body, validationObject);
|
||||||
|
addCustomMessages(_extendMessages);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const isValid = await validation.check();
|
||||||
|
if (!isValid) {
|
||||||
|
req.validationError = formatValidationError(validation.errors);
|
||||||
|
}
|
||||||
|
return next();
|
||||||
|
} catch (error) {
|
||||||
|
req.validationError = error.message;
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
handleValidationErrorForViews: (
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
viewModel,
|
||||||
|
viewPath = '/',
|
||||||
|
fieldsStoreKey,
|
||||||
|
defaultValue = {},
|
||||||
|
) => {
|
||||||
|
const validationError = req.validationError;
|
||||||
|
|
||||||
|
if (validationError) {
|
||||||
|
// Remembers fields if validation error occurs
|
||||||
|
Object.entries(defaultValue).forEach(([key, value]) => {
|
||||||
|
viewModel[fieldsStoreKey][key] = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (typeof validationError === 'string') {
|
||||||
|
viewModel.error = validationError;
|
||||||
|
} else {
|
||||||
|
viewModel.validationError = req.validationError;
|
||||||
|
}
|
||||||
|
return res.render(viewPath, viewModel);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
handleValidationErrorForAPI: (req, res, next) => {
|
||||||
|
const validationError = req.validationError;
|
||||||
|
|
||||||
|
if (validationError) {
|
||||||
|
let error;
|
||||||
|
if (typeof validationError === 'string') {
|
||||||
|
error = validationError;
|
||||||
|
} else {
|
||||||
|
error = req.validationError;
|
||||||
|
}
|
||||||
|
return res.json({ success: false, error });
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
extends layout
|
||||||
|
|
||||||
|
block content
|
||||||
|
h1= message
|
||||||
|
h2= error.status
|
||||||
|
pre #{error.stack}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
extends layout
|
||||||
|
|
||||||
|
block content
|
||||||
|
h1= title
|
||||||
|
p Welcome to #{title}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
doctype html
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title= title
|
||||||
|
link(rel='stylesheet', href='/stylesheets/style.css')
|
||||||
|
body
|
||||||
|
block content
|
||||||
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
Continue day 19 if not done
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
let Sequelize = require('sequelize');
|
let Sequelize = require('sequelize');
|
||||||
const basename = path.basename(__filename);
|
const basename = path.basename(__filename);
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
const config = {
|
const config = {
|
||||||
DB_DATABASE: 'mysql',
|
DB_DATABASE: 'mysql',
|
||||||
DB_USERNAME: 'root',
|
DB_USERNAME: 'root',
|
||||||
@@ -52,7 +53,7 @@ fs.readdirSync(__dirname)
|
|||||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||||
})
|
})
|
||||||
.forEach((file) => {
|
.forEach((file) => {
|
||||||
var model = sequelize['import'](path.join(__dirname, file));
|
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||||
db[model.name] = model;
|
db[model.name] = model;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user