From 8e5aa412d505fff36597c71c7979ce6782875fbb Mon Sep 17 00:00:00 2001 From: diditaditya Date: Tue, 25 Apr 2017 11:46:41 +0700 Subject: [PATCH 1/2] First commit, done with skeleton --- .gitignore | 1 + README.md | 34 +++++++- app.js | 48 +++++++++++ bin/www | 90 +++++++++++++++++++++ config/config.json | 23 ++++++ controllers/.apiController.js.swp | Bin 0 -> 12288 bytes controllers/apiController.js | 5 ++ migrations/20170425041328-create-user.js | 30 +++++++ models/index.js | 36 +++++++++ models/user.js | 14 ++++ package.json | 19 +++++ public/stylesheets/style.css | 8 ++ routes/.api.js.swp | Bin 0 -> 12288 bytes routes/api.js | 10 +++ routes/index.js | 9 +++ routes/users.js | 9 +++ seeders/20170425041424-seed-dummy-users.js | 39 +++++++++ views/error.ejs | 3 + views/index.ejs | 11 +++ 19 files changed, 388 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 app.js create mode 100755 bin/www create mode 100644 config/config.json create mode 100644 controllers/.apiController.js.swp create mode 100644 controllers/apiController.js create mode 100644 migrations/20170425041328-create-user.js create mode 100644 models/index.js create mode 100644 models/user.js create mode 100644 package.json create mode 100644 public/stylesheets/style.css create mode 100644 routes/.api.js.swp create mode 100644 routes/api.js create mode 100644 routes/index.js create mode 100644 routes/users.js create mode 100644 seeders/20170425041424-seed-dummy-users.js create mode 100644 views/error.ejs create mode 100644 views/index.ejs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md index 6379a41..820fb8c 100644 --- a/README.md +++ b/README.md @@ -1 +1,33 @@ -# api-basic \ No newline at end of file +# api-basic + +## Users CRUD + +Implementation of CRUD using all the basic methods + +## REST API + +Routes Method Description +/api/users GET get all users +/api/users/:id GET get a single user +/api/users POST create a user +/api/users/:id DELETE delete a user +/api/users/:id PUT update all data of a user +/api/users/:id PATCH update specific data of a user + +###Filters + +Routes Method Description +/api/users?name="" GET get match in users +/api/users?name="" GET get like in users + + +## Usage + +How to start: +- npm install +- npm start +- npm run dev? + +Access the website via http://localhost:3000 or +API via http://localhost:3000/api + diff --git a/app.js b/app.js new file mode 100644 index 0000000..0ce252c --- /dev/null +++ b/app.js @@ -0,0 +1,48 @@ +var express = require('express'); +var path = require('path'); +var favicon = require('serve-favicon'); +var logger = require('morgan'); +var cookieParser = require('cookie-parser'); +var bodyParser = require('body-parser'); + +var index = require('./routes/index'); +var users = require('./routes/users'); +var api = require('./routes/api'); + +var app = express(); + +// view engine setup +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'ejs'); + +// uncomment after placing your favicon in /public +//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); +app.use(logger('dev')); +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: false })); +app.use(cookieParser()); +app.use(express.static(path.join(__dirname, 'public'))); + +app.use('/', index); +app.use('/users', users); +app.use('/api', api); + +// catch 404 and forward to error handler +app.use(function(req, res, next) { + var err = new Error('Not Found'); + err.status = 404; + next(err); +}); + +// 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; diff --git a/bin/www b/bin/www new file mode 100755 index 0000000..e49e193 --- /dev/null +++ b/bin/www @@ -0,0 +1,90 @@ +#!/usr/bin/env node + +/** + * Module dependencies. + */ + +var app = require('../app'); +var debug = require('debug')('api-basic: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); +} diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..f2974ab --- /dev/null +++ b/config/config.json @@ -0,0 +1,23 @@ +{ + "development": { + "username": "didit", + "password": "didit", + "database": "didit", + "host": "127.0.0.1", + "dialect": "postgres" + }, + "test": { + "username": "root", + "password": null, + "database": "database_test", + "host": "127.0.0.1", + "dialect": "mysql" + }, + "production": { + "username": "root", + "password": null, + "database": "database_production", + "host": "127.0.0.1", + "dialect": "mysql" + } +} diff --git a/controllers/.apiController.js.swp b/controllers/.apiController.js.swp new file mode 100644 index 0000000000000000000000000000000000000000..a5c7bf9a8273e2deadc6552335a75ff7ebd677f2 GIT binary patch literal 12288 zcmeI%K}*9h6bJA(!J|0zwo}x@=3z}Y1qb5E9rPd!Vd^z&GmP!pnxsW9`YqIN>$dGC=lzgy|dzc@W(2i`up(I$%CKCYDCrcQ?_RH3qEzF|y6p<
    +rjUUE1Rwwb2tWV=5P$##{*i#qTJ-4d zyHekIwVqqQ=6Z<<0uX=z1Rwwb2tWV=5P$##AOL|S6c8cNe4XgYMfv~#>-+!RtzTWd zxOjF^)tB&ocx4Dc00Izz00bZa0SG_<0uX?}Ulu5YW>X_IJ7QYi=1R+6*Yo%y4P|V2 j?kc9cdq`!QPIFr|%DYTUW4!Y!(W}dK_UA@*s@&icyz5uk literal 0 HcmV?d00001 diff --git a/controllers/apiController.js b/controllers/apiController.js new file mode 100644 index 0000000..f4be918 --- /dev/null +++ b/controllers/apiController.js @@ -0,0 +1,5 @@ +var express = require('express'); +var router = express.Router(); +var User = require('../models/users'); + + diff --git a/migrations/20170425041328-create-user.js b/migrations/20170425041328-create-user.js new file mode 100644 index 0000000..7055bac --- /dev/null +++ b/migrations/20170425041328-create-user.js @@ -0,0 +1,30 @@ +'use strict'; +module.exports = { + up: function(queryInterface, Sequelize) { + return queryInterface.createTable('Users', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + username: { + type: Sequelize.STRING + }, + password: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: function(queryInterface, Sequelize) { + return queryInterface.dropTable('Users'); + } +}; \ No newline at end of file diff --git a/models/index.js b/models/index.js new file mode 100644 index 0000000..7540dba --- /dev/null +++ b/models/index.js @@ -0,0 +1,36 @@ +'use strict'; + +var fs = require('fs'); +var path = require('path'); +var Sequelize = require('sequelize'); +var basename = path.basename(module.filename); +var env = process.env.NODE_ENV || 'development'; +var config = require(__dirname + '/../config/config.json')[env]; +var db = {}; + +if (config.use_env_variable) { + var sequelize = new Sequelize(process.env[config.use_env_variable]); +} else { + var sequelize = new Sequelize(config.database, config.username, config.password, config); +} + +fs + .readdirSync(__dirname) + .filter(function(file) { + return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); + }) + .forEach(function(file) { + var model = sequelize['import'](path.join(__dirname, file)); + db[model.name] = model; + }); + +Object.keys(db).forEach(function(modelName) { + if (db[modelName].associate) { + db[modelName].associate(db); + } +}); + +db.sequelize = sequelize; +db.Sequelize = Sequelize; + +module.exports = db; diff --git a/models/user.js b/models/user.js new file mode 100644 index 0000000..7789681 --- /dev/null +++ b/models/user.js @@ -0,0 +1,14 @@ +'use strict'; +module.exports = function(sequelize, DataTypes) { + var User = sequelize.define('User', { + username: DataTypes.STRING, + password: DataTypes.STRING + }, { + classMethods: { + associate: function(models) { + // associations can be defined here + } + } + }); + return User; +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..d5d7827 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "api-basic", + "version": "0.0.0", + "private": true, + "scripts": { + "start": "node ./bin/www" + }, + "dependencies": { + "body-parser": "~1.17.1", + "cookie-parser": "~1.4.3", + "debug": "~2.6.3", + "ejs": "~2.5.6", + "express": "~4.15.2", + "morgan": "~1.8.1", + "pg": "^6.1.5", + "sequelize": "^3.30.4", + "serve-favicon": "~2.4.2" + } +} diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css new file mode 100644 index 0000000..9453385 --- /dev/null +++ b/public/stylesheets/style.css @@ -0,0 +1,8 @@ +body { + padding: 50px; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +} + +a { + color: #00B7FF; +} diff --git a/routes/.api.js.swp b/routes/.api.js.swp new file mode 100644 index 0000000000000000000000000000000000000000..a8aafad5cc90ef48936fc709594883ca1888d711 GIT binary patch literal 12288 zcmeI&&2H2%5C`xnHxOTPsoz+$qAn_9L z7`y>j?%a74#CRJdL=Sr|{3A`Q*b{sFyG8Nd^6Td>_;K-w^tws(<@?V!^Ox_@!U9q0 zN*7cAjBDGD-O$z5j5K;bozQI7gLWiU4Ca{~97Q)?GuwwY2(Ox5kt1F)oAyNSwsA{) zHn@TDbsHt5j^b^YwF<@|0D*G?ZQNg4yG9RJSC-S*-8;8={?^X9G;}}!0uX=z1Rwwb z2tWV=|E@r6=IDcN?^3tLD_uMHSKB?JfdB*`009U<00Izz00bZa0SG|g0t(oY=-YLo zt2eZG{r~^_`~QbNf7SS`@k!&O#;!(BXHJ_**5^}>E)Hd#v#m*^2_fT@ zeD-E>ATiI3rtP=YAnFnM+{!h@^wKP!+PZbm|({Xv9cf(!ppA&UmX@Xx=&3=#o literal 0 HcmV?d00001 diff --git a/routes/api.js b/routes/api.js new file mode 100644 index 0000000..a79c222 --- /dev/null +++ b/routes/api.js @@ -0,0 +1,10 @@ +var express = require('express'); +var router = express.Router(); +var User = require('../models/users'); + +/* GET home page. */ +router.get('/', function(req, res, next) { + res.render('index', { title: 'Express' }); +}); + +module.exports = router; diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..ecca96a --- /dev/null +++ b/routes/index.js @@ -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; diff --git a/routes/users.js b/routes/users.js new file mode 100644 index 0000000..623e430 --- /dev/null +++ b/routes/users.js @@ -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; diff --git a/seeders/20170425041424-seed-dummy-users.js b/seeders/20170425041424-seed-dummy-users.js new file mode 100644 index 0000000..6d66ecd --- /dev/null +++ b/seeders/20170425041424-seed-dummy-users.js @@ -0,0 +1,39 @@ +'use strict'; + +module.exports = { + up: function (queryInterface, Sequelize) { + /* + Add altering commands here. + Return a promise to correctly handle asynchronicity. + + Example: + return queryInterface.bulkInsert('Person', [{ + name: 'John Doe', + isBetaMember: false + }], {}); + */ + + return queryInterface.bulkInsert('Users', [{ + username: 'johndoe', + password: 'johnisdoe', + createdAt: new Date(), + updatedAt: new Date() + }, { + username: 'janedoe', + password: 'janeisdoe', + createdAt: new Date(), + updatedAt: new Date() + }]); + + }, + + down: function (queryInterface, Sequelize) { + /* + Add reverting commands here. + Return a promise to correctly handle asynchronicity. + + Example: + return queryInterface.bulkDelete('Person', null, {}); + */ + } +}; diff --git a/views/error.ejs b/views/error.ejs new file mode 100644 index 0000000..7cf94ed --- /dev/null +++ b/views/error.ejs @@ -0,0 +1,3 @@ +

    <%= message %>

    +

    <%= error.status %>

    +
    <%= error.stack %>
    diff --git a/views/index.ejs b/views/index.ejs new file mode 100644 index 0000000..7b7a1d6 --- /dev/null +++ b/views/index.ejs @@ -0,0 +1,11 @@ + + + + <%= title %> + + + +

    <%= title %>

    +

    Welcome to <%= title %>

    + + From 9dc3f8912ab31c6a82f646391e69aa91ac875a0d Mon Sep 17 00:00:00 2001 From: diditaditya Date: Tue, 25 Apr 2017 19:09:21 +0700 Subject: [PATCH 2/2] api-basic crud is done --- README.md | 45 +++++-------- app.js | 2 - bin/www | 2 +- config/config.json | 16 +---- controllers/.apiController.js.swp | Bin 12288 -> 0 bytes controllers/apiController.js | 5 -- controllers/user.js | 62 ++++++++++++++++++ ...-user.js => 20170425074439-create-user.js} | 3 + models/user.js | 3 +- package.json | 4 +- routes/.api.js.swp | Bin 12288 -> 0 bytes routes/api.js | 10 --- routes/index.js | 7 ++ seeders/20170425041424-seed-dummy-users.js | 39 ----------- 14 files changed, 97 insertions(+), 101 deletions(-) delete mode 100644 controllers/.apiController.js.swp delete mode 100644 controllers/apiController.js create mode 100644 controllers/user.js rename migrations/{20170425041328-create-user.js => 20170425074439-create-user.js} (92%) delete mode 100644 routes/.api.js.swp delete mode 100644 routes/api.js delete mode 100644 seeders/20170425041424-seed-dummy-users.js diff --git a/README.md b/README.md index 820fb8c..918dbaf 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,24 @@ -# api-basic +# API-BASIC -## Users CRUD +API-Basic is a simple example of CRUD using the API. -Implementation of CRUD using all the basic methods +## Routes -## REST API +| Route | HTTP method | Description| +| ---------- | ------------------- | ---------- | +| /api/users | GET | Get all the users info | +| /api/users/:id | GET | Get a single user info | +| /api/users/ | POST | Create a user | +| /api/users/:id | DELETE | Delete a user | +| /api/users/:id | PUT | Update a user with new info | -Routes Method Description -/api/users GET get all users -/api/users/:id GET get a single user -/api/users POST create a user -/api/users/:id DELETE delete a user -/api/users/:id PUT update all data of a user -/api/users/:id PATCH update specific data of a user -###Filters - -Routes Method Description -/api/users?name="" GET get match in users -/api/users?name="" GET get like in users - - -## Usage - -How to start: -- npm install -- npm start -- npm run dev? - -Access the website via http://localhost:3000 or -API via http://localhost:3000/api +Create user requires {username:'', password:'', role:<'admin' or 'user'>} +## How to Use: +Install the dependencies first and then start the app as follows: +```sh +$ npm install +$ npm start +``` +Access the website via HTTP://localhost:3000, running the app in Postman is recommended diff --git a/app.js b/app.js index 0ce252c..b2a5037 100644 --- a/app.js +++ b/app.js @@ -7,7 +7,6 @@ var bodyParser = require('body-parser'); var index = require('./routes/index'); var users = require('./routes/users'); -var api = require('./routes/api'); var app = express(); @@ -25,7 +24,6 @@ app.use(express.static(path.join(__dirname, 'public'))); app.use('/', index); app.use('/users', users); -app.use('/api', api); // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/bin/www b/bin/www index e49e193..fea87fd 100755 --- a/bin/www +++ b/bin/www @@ -5,7 +5,7 @@ */ var app = require('../app'); -var debug = require('debug')('api-basic:server'); +var debug = require('debug')('api-auth:server'); var http = require('http'); /** diff --git a/config/config.json b/config/config.json index f2974ab..5eb9a2d 100644 --- a/config/config.json +++ b/config/config.json @@ -2,22 +2,8 @@ "development": { "username": "didit", "password": "didit", - "database": "didit", + "database": "api-basic", "host": "127.0.0.1", "dialect": "postgres" - }, - "test": { - "username": "root", - "password": null, - "database": "database_test", - "host": "127.0.0.1", - "dialect": "mysql" - }, - "production": { - "username": "root", - "password": null, - "database": "database_production", - "host": "127.0.0.1", - "dialect": "mysql" } } diff --git a/controllers/.apiController.js.swp b/controllers/.apiController.js.swp deleted file mode 100644 index a5c7bf9a8273e2deadc6552335a75ff7ebd677f2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI%K}*9h6bJA(!J|0zwo}x@=3z}Y1qb5E9rPd!Vd^z&GmP!pnxsW9`YqIN>$dGC=lzgy|dzc@W(2i`up(I$%CKCYDCrcQ?_RH3qEzF|y6p<
      +rjUUE1Rwwb2tWV=5P$##{*i#qTJ-4d zyHekIwVqqQ=6Z<<0uX=z1Rwwb2tWV=5P$##AOL|S6c8cNe4XgYMfv~#>-+!RtzTWd zxOjF^)tB&ocx4Dc00Izz00bZa0SG_<0uX?}Ulu5YW>X_IJ7QYi=1R+6*Yo%y4P|V2 j?kc9cdq`!QPIFr|%DYTUW4!Y!(W}dK_UA@*s@&icyz5uk diff --git a/controllers/apiController.js b/controllers/apiController.js deleted file mode 100644 index f4be918..0000000 --- a/controllers/apiController.js +++ /dev/null @@ -1,5 +0,0 @@ -var express = require('express'); -var router = express.Router(); -var User = require('../models/users'); - - diff --git a/controllers/user.js b/controllers/user.js new file mode 100644 index 0000000..38c693c --- /dev/null +++ b/controllers/user.js @@ -0,0 +1,62 @@ +var db = require('../models'); + +let control = { + findAll: (req, res) => { + db.User.findAll().then((data) => { + res.send(data); + }).catch((err) => { + res.send(err); + }); + }, + findById: (req, res) => { + let id = req.params.id; + db.User.findById(id).then((data) => { + res.send(data); + }).catch((err) => { + res.send(err); + }); + }, + create: (req, res) => { + let username = req.body.username; + let password = req.body.password; + let role = req.body.role; + if (username && password && role) { + db.User.create({username:username, password:password, role:role}) + .then((data) => { + res.send(data); + }).catch((err) => { + res.send(err); + }); + } else { + res.send('username, password, role must not be empty') + } + }, + delete: (req, res) => { + let userId = req.params.id; + db.User.destroy({where: {id:userId}}) + .then((destroyed) => { + res.json(destroyed); + }).catch((err) => { + res.send(err); + }); + }, + update: (req, res) => { + let userId = req.params.id; + let newUsername = req.body.username; + let newPassword = req.body.password; + db.User.update({username:newUsername, password:newPassword}, {where:{id:userId}}) + .then((updated) => { + res.send(updated); + }).catch((err) => { + res.send(err); + }); + } + +} + +module.exports = control; + +/* +user:gurame pass:bakar role:user +user:ivan pass:habibie role:admin +*/ diff --git a/migrations/20170425041328-create-user.js b/migrations/20170425074439-create-user.js similarity index 92% rename from migrations/20170425041328-create-user.js rename to migrations/20170425074439-create-user.js index 7055bac..228545b 100644 --- a/migrations/20170425041328-create-user.js +++ b/migrations/20170425074439-create-user.js @@ -14,6 +14,9 @@ module.exports = { password: { type: Sequelize.STRING }, + role: { + type: Sequelize.STRING + }, createdAt: { allowNull: false, type: Sequelize.DATE diff --git a/models/user.js b/models/user.js index 7789681..a638451 100644 --- a/models/user.js +++ b/models/user.js @@ -2,7 +2,8 @@ module.exports = function(sequelize, DataTypes) { var User = sequelize.define('User', { username: DataTypes.STRING, - password: DataTypes.STRING + password: DataTypes.STRING, + role: DataTypes.STRING }, { classMethods: { associate: function(models) { diff --git a/package.json b/package.json index d5d7827..2f104b3 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "api-basic", + "name": "api-auth", "version": "0.0.0", "private": true, "scripts": { @@ -11,7 +11,9 @@ "debug": "~2.6.3", "ejs": "~2.5.6", "express": "~4.15.2", + "jsonwebtoken": "^7.4.0", "morgan": "~1.8.1", + "password-hash": "^1.2.2", "pg": "^6.1.5", "sequelize": "^3.30.4", "serve-favicon": "~2.4.2" diff --git a/routes/.api.js.swp b/routes/.api.js.swp deleted file mode 100644 index a8aafad5cc90ef48936fc709594883ca1888d711..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI&&2H2%5C`xnHxOTPsoz+$qAn_9L z7`y>j?%a74#CRJdL=Sr|{3A`Q*b{sFyG8Nd^6Td>_;K-w^tws(<@?V!^Ox_@!U9q0 zN*7cAjBDGD-O$z5j5K;bozQI7gLWiU4Ca{~97Q)?GuwwY2(Ox5kt1F)oAyNSwsA{) zHn@TDbsHt5j^b^YwF<@|0D*G?ZQNg4yG9RJSC-S*-8;8={?^X9G;}}!0uX=z1Rwwb z2tWV=|E@r6=IDcN?^3tLD_uMHSKB?JfdB*`009U<00Izz00bZa0SG|g0t(oY=-YLo zt2eZG{r~^_`~QbNf7SS`@k!&O#;!(BXHJ_**5^}>E)Hd#v#m*^2_fT@ zeD-E>ATiI3rtP=YAnFnM+{!h@^wKP!+PZbm|({Xv9cf(!ppA&UmX@Xx=&3=#o diff --git a/routes/api.js b/routes/api.js deleted file mode 100644 index a79c222..0000000 --- a/routes/api.js +++ /dev/null @@ -1,10 +0,0 @@ -var express = require('express'); -var router = express.Router(); -var User = require('../models/users'); - -/* GET home page. */ -router.get('/', function(req, res, next) { - res.render('index', { title: 'Express' }); -}); - -module.exports = router; diff --git a/routes/index.js b/routes/index.js index ecca96a..e23c00e 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,9 +1,16 @@ var express = require('express'); var router = express.Router(); +var userControl = require('../controllers/user'); /* GET home page. */ router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); }); +router.get('/api/users', userControl.findAll); +router.post('/api/users', userControl.create); +router.get('/api/users/:id', userControl.findById); +router.delete('/api/users/:id', userControl.delete); +router.put('/api/users/:id', userControl.update); + module.exports = router; diff --git a/seeders/20170425041424-seed-dummy-users.js b/seeders/20170425041424-seed-dummy-users.js deleted file mode 100644 index 6d66ecd..0000000 --- a/seeders/20170425041424-seed-dummy-users.js +++ /dev/null @@ -1,39 +0,0 @@ -'use strict'; - -module.exports = { - up: function (queryInterface, Sequelize) { - /* - Add altering commands here. - Return a promise to correctly handle asynchronicity. - - Example: - return queryInterface.bulkInsert('Person', [{ - name: 'John Doe', - isBetaMember: false - }], {}); - */ - - return queryInterface.bulkInsert('Users', [{ - username: 'johndoe', - password: 'johnisdoe', - createdAt: new Date(), - updatedAt: new Date() - }, { - username: 'janedoe', - password: 'janeisdoe', - createdAt: new Date(), - updatedAt: new Date() - }]); - - }, - - down: function (queryInterface, Sequelize) { - /* - Add reverting commands here. - Return a promise to correctly handle asynchronicity. - - Example: - return queryInterface.bulkDelete('Person', null, {}); - */ - } -};