Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Javascript Directory + Beginner Express.js Tutorial #35

Merged
merged 4 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions JavaScript/Node.js/Express.js/Beginner API/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
23 changes: 23 additions & 0 deletions JavaScript/Node.js/Express.js/Beginner API/README.md
Original file line number Diff line number Diff line change
@@ -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!
```
38 changes: 38 additions & 0 deletions JavaScript/Node.js/Express.js/Beginner API/app.js
Original file line number Diff line number Diff line change
@@ -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!
});

16 changes: 16 additions & 0 deletions JavaScript/Node.js/Express.js/Beginner API/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}