Skip to content

Commit

Permalink
added code and dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Dec 18, 2022
0 parents commit 50c2bae
Show file tree
Hide file tree
Showing 7 changed files with 5,878 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

node_modules/
deb
echo
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
EXPOSE 8000
CMD ["node","app.js"]
88 changes: 88 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const express = require('express'),
bodyParser = require('body-parser'),
// In order to use PUT HTTP verb to edit item
methodOverride = require('method-override'),
// Mitigate XSS using sanitizer
sanitizer = require('sanitizer'),
app = express(),
port = 8000

app.use(bodyParser.urlencoded({
extended: false
}));
// https: //github.com/expressjs/method-override#custom-logic
app.use(methodOverride(function (req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
let method = req.body._method;
delete req.body._method;
return method
}
}));


let todolist = [];

/* The to do list and the form are displayed */
app.get('/todo', function (req, res) {
res.render('todo.ejs', {
todolist,
clickHandler: "func1();"
});
})

/* Adding an item to the to do list */
.post('/todo/add/', function (req, res) {
// Escapes HTML special characters in attribute values as HTML entities
let newTodo = sanitizer.escape(req.body.newtodo);
if (req.body.newtodo != '') {
todolist.push(newTodo);
}
res.redirect('/todo');
})

/* Deletes an item from the to do list */
.get('/todo/delete/:id', function (req, res) {
if (req.params.id != '') {
todolist.splice(req.params.id, 1);
}
res.redirect('/todo');
})

// Get a single todo item and render edit page
.get('/todo/:id', function (req, res) {
let todoIdx = req.params.id;
let todo = todolist[todoIdx];

if (todo) {
res.render('edititem.ejs', {
todoIdx,
todo,
clickHandler: "func1();"
});
} else {
res.redirect('/todo');
}
})

// Edit item in the todo list
.put('/todo/edit/:id', function (req, res) {
let todoIdx = req.params.id;
// Escapes HTML special characters in attribute values as HTML entities
let editTodo = sanitizer.escape(req.body.editTodo);
if (todoIdx != '' && editTodo != '') {
todolist[todoIdx] = editTodo;
}
res.redirect('/todo');
})
/* Redirects to the to do list if the page requested is not found */
.use(function (req, res, next) {
res.redirect('/todo');
})

.listen(port, function () {
// Logging to console
console.log(`Todolist running on http://0.0.0.0:${port}`)
});
// Export app
module.exports = app;
Loading

0 comments on commit 50c2bae

Please sign in to comment.