Last active
March 4, 2020 21:36
-
-
Save jaquiel/7e45947bb6e7288eda80470c3d00dc93 to your computer and use it in GitHub Desktop.
Node.js sever examples with express.js framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Node.js server | |
| * with express.js framework | |
| * and routing | |
| */ | |
| const express = require('express') | |
| const app = express() | |
| app.use(express.json()) | |
| //app.all('/', (req, res, next) => { | |
| // res.send('Root route') | |
| // next() | |
| //}) | |
| const fruits = ['apple','banana','orange'] | |
| app.get('/fruits', (req, res) => { | |
| return res.json(fruits) | |
| }) | |
| app.get('/fruits/:index', (req, res) => { | |
| const { index } = req.params | |
| return res.json(fruits[index]) | |
| }) | |
| app.post('/fruits', (req, res) => { | |
| const { fruit } = req.body | |
| fruits.push(fruit) | |
| return res.json(fruits) | |
| }) | |
| app.put('/fruits/:index', (req, res) =>{ | |
| const { index } = req.params | |
| const { fruit } = req.body | |
| fruits[index] = fruit | |
| return res.json(fruits) | |
| }) | |
| app.delete('/fruits/:index', (req, res) =>{ | |
| const { index } = req.params | |
| fruits.splice(index, 1) | |
| return res.json(fruits) | |
| }) | |
| app.listen( 8080, () => { | |
| console.log('running on port 8080') | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Node.js server | |
| * Minimal example | |
| * with express.js framework | |
| */ | |
| const express = require('express') | |
| express().all('/', (req, res) => { | |
| res.send('Hello world!') | |
| }).listen(8080, ()=>{ | |
| console.log('runing server on port 8080...') | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment