diff --git a/README.md b/README.md index e478e8a..47a76d9 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,6 @@ This project is gonna be awesome! -Redwanuzzaman CSE-1692 \ No newline at end of file +Redwanuzzaman CSE-1692 + +Saiful Islam Rasel -> CSE-17045 \ No newline at end of file diff --git a/api/routes/customer.js b/api/routes/customer.js new file mode 100644 index 0000000..f4fd2ed --- /dev/null +++ b/api/routes/customer.js @@ -0,0 +1,63 @@ + + const express = require('express'); + const router = express.Router(); + //array declare to store all userDetails + const userDetailsList=[]; + + router.post('/registration',(req, res, next) =>{ + var userDetails ={ + userId: req.body.userId, + userName: req.body.userName + } + + //add individual user userDetailsList array + userDetailsList.push(userDetails); + console.log(userDetails); + console.log(userDetailsList); + + res.status(201).json({ + message: 'Handlaing Post request to /userDetails', + userDetails: userDetails + }); + }); + + //get all register user + router.get('/getUsersList', (req, res, next) => { + res.status(201).json({ + usersList: userDetailsList + }); + }); + + //retrieve the match user .. + router.get('/getUser/:userId', (req, res, next) =>{ + var user = {}; + for( var i = 0; i < userDetailsList.length; i++){ + if(req.params.userId == userDetailsList[i].userId){ + user = userDetailsList[i]; + break; + } + } + + res.status(200).json({ + message: 'userDetails', + userDetail: user + }); + }); + + //delete the match user from userDetailsList + router.delete('/deleteUser/:userId', (req, res, next)=>{ + for( var i = 0; i < userDetailsList.length; i++){ + if(req.params.userId == userDetailsList[i].userId){ + userDetailsList.splice(i,1); + break; + } + } + res.status(201).json({ + message: 'following id is deleted', + id: req.params.userId + }); + + }); + + + module.exports = router; \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..1d7467b --- /dev/null +++ b/app.js @@ -0,0 +1,14 @@ + const express = require('express'); + const app = express(); + + //use to parse body into incoming request //have to install it by "npm install body-parser" command + const bodyParser = require('body-parser'); + //include customer.js + const customerRoutes = require('./api/routes/customer'); + + //use to parse body into incoming request + app.use(bodyParser.urlencoded({extended: false})); + app.use(bodyParser.json()); + app.use('/customer', customerRoutes); + + module.exports = app; \ No newline at end of file diff --git a/index.js b/index.js index 140d081..0023869 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,47 @@ // importing http package -const http = require('http') +const http = require('http'); +const express = require('express'); +const app = express(); + + app.get('/name/:name', (req, res) => { + console.log(req.url); + res.end('HI !' + req.params.name); + }); + + app.get('/item', (req, res) =>{ + console.log(req.url); + res.end('you are in the item page !'); + }); + + app.get('/', (req, res) =>{ + console.log(req.url); + res.end('This is Home Page'); + }); + /* error Handling if bad Request */ + app.use((req, res, next) =>{ + const error = new Error('Not found'); + error.status = 404; + next(error); + }); + app.use((error, req, res, next) => { + res.json({ + error:{ + errorMessage: error.message, + notice: 'plz try to access with the valid request' + } + }); + }); + const server = http.createServer(app).listen(3000); + console.log('Server is Running having port ' +server.address().port); + +/* http.createServer(app).listen(3000); */ + /* // creating a basic server const server = http.createServer( function (req, res) { console.log("the req URL is: ") console.log(req.url) - + // basic routing if (req.url == "/") { res.end("this is the homepage") @@ -19,4 +55,4 @@ const server = http.createServer( function (req, res) { }).listen(3000, 'localhost', function() { console.log("the port is: ") console.log(server.address().port) -}) +}) */ diff --git a/package.json b/package.json index 4123e1b..fb7055e 100644 --- a/package.json +++ b/package.json @@ -15,5 +15,9 @@ "bugs": { "url": "https://github.com/pcIST/API_Development/issues" }, - "homepage": "https://github.com/pcIST/API_Development#readme" + "homepage": "https://github.com/pcIST/API_Development#readme", + "dependencies": { + "body-parser": "^1.18.3", + "express": "^4.16.3" + } } diff --git a/server.js b/server.js new file mode 100644 index 0000000..6356811 --- /dev/null +++ b/server.js @@ -0,0 +1,8 @@ + // importing http package + const http = require('http'); + + //included app.js file in this server + const app = require('./app'); + const server = http.createServer(app).listen(3000); + + console.log('Server is Running having port ' +server.address().port); \ No newline at end of file