coding-blocks-archives/EventManagerNodejs

Name: EventManagerNodejs

Owner: Coding Blocks Archives

Description: null

Created: 2017-06-05 05:36:35.0

Updated: 2017-06-13 05:38:55.0

Pushed: 2017-06-14 11:58:31.0

Homepage: null

Size: 22

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Event Management Website (in NodeJS)

Step1: Create Basic Express App

Initialise NPM module and Install Express

init
install --save express

Contents of - server.js

t express = require('express');

t app = express();

get('/', (req, res) => {
es.send('Hello World')


listen(2345, function () {
console.log("Server started on http://localhost:2345");

Step2: Add stub API paths (Events and Teachers)

routes/events.js

t router = require('express').Router;

t route = router();

e.get('/', (req, res) => {
es.send("GET Array of Events")


e.post('/new', (req, res) => {
es.send("POST Add new Event")


le.exports = route;

routes/index.js

t router = require('express').Router;

t route = router();

e.use('/events', require('./events'));
e.use('/users', require('./users'));

le.exports = route;

Added to server.js

use('/api', require('./routes/api'));
Step3: Install some more dependencies we'll need

Install via NPM

install --save body-parser sequelize mysql2

Also we have to install the MySQL community edition to our computer Use this page to download it - https://dev.mysql.com/downloads/mysql/

Step4: Setup the DB

Run MySQL as root mysql -u root -p and create a DB and a User

TE DATABASE eventman;
TE USER eventadmin IDENTIFIED BY 'eventpass';
eventman;
T ALL PRIVILEGES ON eventman to eventadmin@'%';

Login using the new user and verify the database is accessible, first login with mysql -u eventadmin -p

eventman;
Step5: Add DB models and functions

Add db/models.js where the tables are defined

t Sequelize = require('sequelize');

t db = new Sequelize('eventman', 'eventadmin', 'eventpass', {
host: 'localhost',
dialect: 'mysql'


t Event = db.define('event', {
id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
},
name: Sequelize.STRING,
startTime: Sequelize.DATE,
endTime: Sequelize.DATE,
hostMessage: Sequelize.STRING,
venue: Sequelize.STRING


t User = db.define('user', {
id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
},
name: Sequelize.STRING,
email: Sequelize.STRING


ync()
.then(() => {
    console.log("Database Synchronised");
})
.catch((err) => {
    console.log("Error setting up Database");
    console.error(err);
});

le.exports = {
db,
models: {
    User,
    Event
}

Also add a db/actions.js where the functions reside

t db = require('./models').db;
t models = require('./models').models;

tion addUser(name, email) {
return models.User.create({
    name, email
})


le.exports = {
addUser


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.