Created
August 1, 2023 19:02
-
-
Save heerdyes/36f2362e5650e284404c40a7fededbbe to your computer and use it in GitHub Desktop.
express js backend
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
const express = require('express'); | |
const app = express(); | |
const port = 3000; | |
let kvs = [ | |
{ | |
ccode: 'JS101', | |
cname: 'javascript fundamentals' | |
}, | |
{ | |
ccode: 'RB101', | |
cname: 'ruby fundamentals' | |
}, | |
{ | |
ccode: 'PY101', | |
cname: 'python fundamentals' | |
} | |
]; | |
app.use(express.static('public')); | |
app.use(express.json()); | |
app.get('/courses', (req, res) => { | |
res.send(kvs); | |
}); | |
app.post('/courses', (req, res) => { | |
let ob = req.body; | |
console.log('[post_rcvd]', ob); | |
kvs.push(ob); | |
res.send({ status: 'OK' }); | |
}); | |
app.get('/rmcourse/:cid', (req, res) => { | |
let courseid = Number(req.params.cid); | |
console.log('[del] cid: ', courseid); | |
let nkvs = []; | |
for(let i = 0; i < kvs.length; i++) { | |
if (i !== courseid) { | |
nkvs.push(kvs[i]); | |
} | |
} | |
kvs = nkvs; | |
res.send({ status: 'OK' }); | |
}); | |
app.listen(port, () => console.log(`server listening on port: ${port}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment