Skip to content

Instantly share code, notes, and snippets.

@heerdyes
Created August 1, 2023 19:02
Show Gist options
  • Save heerdyes/36f2362e5650e284404c40a7fededbbe to your computer and use it in GitHub Desktop.
Save heerdyes/36f2362e5650e284404c40a7fededbbe to your computer and use it in GitHub Desktop.
express js backend
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