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
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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");
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'));
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/
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;
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