diff --git a/JavaScript/Node.js/Express.js/Beginner API/.gitignore b/JavaScript/Node.js/Express.js/Beginner API/.gitignore new file mode 100644 index 0000000..25c8fdb --- /dev/null +++ b/JavaScript/Node.js/Express.js/Beginner API/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json \ No newline at end of file diff --git a/JavaScript/Node.js/Express.js/Beginner API/README.md b/JavaScript/Node.js/Express.js/Beginner API/README.md new file mode 100644 index 0000000..a4da559 --- /dev/null +++ b/JavaScript/Node.js/Express.js/Beginner API/README.md @@ -0,0 +1,23 @@ +# Beginner Express.js Server + +This is a quick tutorial for beginners to learn Express.js + +- You will need to have [Node.js](https://nodejs.org/en/) installed prior to starting this tutorial. +_(If this is your first time downloading Node.js, restart your PC after install, or else NPM commands will not work.)_ + +- I recommend downloading the [Postman](https://www.getpostman.com/) client in order to test your endpoints. + +## How to start the server +Using your terminal either via your OS or VS Code, run the two commands below, in this order: +1. **npm install** +2. **npm start** + +Once you've completed the setup, you should see the following in the console after running the above commands: +``` +[nodemon] 1.19.3 +[nodemon] to restart at any time, enter `rs` +[nodemon] watching dir(s): *.* +[nodemon] watching extensions: js,mjs,json +[nodemon] starting `node app.js` +Server started successfully! +``` \ No newline at end of file diff --git a/JavaScript/Node.js/Express.js/Beginner API/app.js b/JavaScript/Node.js/Express.js/Beginner API/app.js new file mode 100644 index 0000000..0142cda --- /dev/null +++ b/JavaScript/Node.js/Express.js/Beginner API/app.js @@ -0,0 +1,38 @@ +const express = require('express'); +const app = express(); + +app.listen(3000, () => console.log('Server started successfully!')); +app.use(express.json()); + +// cd into the Node.js\APIs\Express.js\Beginner folder, and run 'npm install', then 'npm start' to start the server! + +// Simple GET endpoint +app.get('/users', (req, res) => { + const userList = [ + {name: 'Mike', age: 23}, + {name: 'Jim', age: 41}, + {name: 'Terra', age: 19}, + {name: 'Richard', age: 32}, + {name: 'Alex', age: 56}, + ]; + + res.send(userList); + // GET localhost:3000/users = [{"name": "Mike", "age": 23}, {"name": "Jim", "age": 41},..] +}); + +// Simple POST endpoint +app.post('/createUser', (req, res) => { + const userList = [ + {name: 'Mike', age: 23}, + {name: 'Jim', age: 41}, + {name: 'Terra', age: 19}, + {name: 'Richard', age: 32}, + {name: 'Alex', age: 56}, + ]; + + userList.push({name: req.body.name, age: req.body.age}) + + res.send(userList) + // POST localhost:3000/createUser using {"name": "Bob", "age": 44} to add him to the list! +}); + diff --git a/JavaScript/Node.js/Express.js/Beginner API/package.json b/JavaScript/Node.js/Express.js/Beginner API/package.json new file mode 100644 index 0000000..10f4f43 --- /dev/null +++ b/JavaScript/Node.js/Express.js/Beginner API/package.json @@ -0,0 +1,16 @@ +{ + "name": "Beginner", + "version": "1.0.0", + "description": "", + "main": "app.js", + "scripts": { + "start": "nodemon app.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "express": "^4.17.1", + "nodemon": "^1.19.3" + } +}