├── .gitignore ├── README.md ├── db └── db_connection.js ├── index.js ├── package.json └── routes └── admin.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | /node_modules 3 | package-lock.json 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Express-API 2 | -------------------------------------------------------------------------------- /db/db_connection.js: -------------------------------------------------------------------------------- 1 | var mysql = require('mysql') 2 | var database; 3 | 4 | function getConnection(){ 5 | if (database){ 6 | database = mysql.createConnection( 7 | { 8 | host:'localhost', 9 | user:'root', 10 | password:'1023', 11 | database:'expressjsAPI' 12 | } 13 | ); 14 | } 15 | return database; 16 | } 17 | 18 | module.exports = getConnection(); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | var admin = require('./routes/admin') 3 | 4 | const app = express() 5 | const port = 3000 6 | 7 | app.use('/admin',admin); 8 | 9 | app.listen(port,() =>{ 10 | console.log("Express API running on port "+port) 11 | }) -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-api", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Mindula Dilthushan", 10 | "license": "ISC", 11 | "dependencies": { 12 | "express": "^4.17.1", 13 | "mysql": "^2.18.1", 14 | "nodemon": "^2.0.13" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /routes/admin.js: -------------------------------------------------------------------------------- 1 | var express = require('express') 2 | const connection = require('../db/db_connection') 3 | 4 | var router = express.Router() 5 | 6 | //Get Method-------------------------------------------- 7 | router.get('/', function (req, res) { 8 | connection.query('SELECT * FROM Admin', function (err, rows) { 9 | if (err) throw err 10 | res.json(rows) 11 | }) 12 | }) 13 | 14 | //Post Method-------------------------------------------- 15 | router.post('/', function (req, res) { 16 | connection.query('INSERT INTO Admin VALUES(?,?,?)', [req.headers.adminId, req.headers.adminName, req.headers.adminEmail], function (err, rows) { 17 | if (err) throw err 18 | res.json(rows); 19 | }) 20 | }) 21 | 22 | //Delete Method-------------------------------------------- 23 | router.delete('/', function (req, res) { 24 | connection.query('DELETE FROM Admin WHERE adminId=?', req.headers.id, function (err, rows) { 25 | if (err) throw err 26 | res.json(rows) 27 | }) 28 | }) 29 | 30 | //Put Method-------------------------------------------- 31 | router.put('/', function (req, res) { 32 | connection.query('UPDATE Admin set adminName=?, adminEmail=? WHERE adminId=?', [req.headers.adminName, req.headers.adminEmail,req.headers.adminId], function (err, rows) { 33 | if (err) throw err 34 | res.json(rows) 35 | }) 36 | }) 37 | 38 | module.exports = router --------------------------------------------------------------------------------