Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 158270c

Browse files
Refactored the code
1 parent 8be3acf commit 158270c

File tree

4 files changed

+50
-79
lines changed

4 files changed

+50
-79
lines changed

‎js_ts/nodejs-ts/README.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
1-
# Nodejs TS demo application
1+
# NodeJS demo application
22

3-
## TODO application
3+
## Functionality
44

5-
### Usage of application
5+
It is simple nodeJS express TODO application, which has several functionalities, such as, adding,
6+
deleting single TODOs, and listing TODOs. Additional functionality includes, deleting the whole TODO list json file and command execution on the server. The application creates a `todo.json` file in the root folder of the application as a database to save all added TODOs.
67

7-
It is simple nodejs express TODO application which has several functionalities, such as, adding,
8-
deleting, listing TODOs, deleting whole json file and command execution in the server. The application creates
9-
todo.json file in the root folder of the application.
10-
11-
Endpoints:
12-
13-
`/api/add?todo=<todo>&deadline=<deadline>`
14-
15-
`/api/delete?id=<id>`
8+
Available endpoints:
9+
10+
`/api/add?todo=&deadline=`
11+
12+
`/api/delete?id=`
1613

1714
`/api/list`
1815

1916
`/api/deleteList`
2017

21-
`/api/server?command=<command>`
22-
23-
24-
25-
26-
27-
28-
18+
`/api/server?command=`

‎js_ts/nodejs-ts/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,40 @@ app.post('/api/add', (req: Request, res: Response) => {
1717

1818
if (status) {
1919
res.status(201)
20-
res.send("Added successfully!");
20+
res.send("Added successfully!\n");
2121
}
2222
res.status(400)
23-
res.send("You are doing something wrong!");
23+
res.send("You are doing something wrong!\n");
2424

2525
});
2626

2727
app.get('/api/list', (req: Request, res: Response) => {
2828

2929
const todos = listFile();
3030
res.status(200)
31-
if (todos[0]) {
32-
res.send("Here is the todos:\n" + todos[1]);
31+
if (todos!==null&&todos.length>0) {
32+
res.send("Here is the todos:\n" + todos);
3333
}
34-
res.send("You are doing something wrong!")
34+
res.send("There is nothing to list!\n")
3535
});
3636

3737
app.post('/api/delete', (req: Request, res: Response) => {
3838
const id: string = req.query.id;
3939
res.status(200)
4040
if (deleteEntry(id)) {
41-
res.send("TODO is deleted successfully!");
41+
res.send("TODO is deleted successfully!\n");
4242
}
43-
res.send("You are doing something wrong!")
43+
res.send("You should enter valid id...\n")
4444

4545
});
4646

4747

4848
app.get('/api/deleteList', (req: Request, res: Response) => {
4949
res.status(200)
5050
if (deleteFile()) {
51-
res.send("The database is deleted!")
51+
res.send("The database is deleted!\n")
5252
}
53-
res.send("You are doing something wrong!");
53+
res.send("There is not a database to delete...\n");
5454

5555
});
5656

‎js_ts/nodejs-ts/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "nodejs-ts",
2+
"name": "TODO list server",
33
"version": "1.0.0",
4-
"description": "",
4+
"description": "NodeJS tutorial example",
55
"main": "dist/index.js",
66
"scripts": {
77
"start": "tsc && node dist/app.js"
@@ -10,11 +10,8 @@
1010
"author": "",
1111
"license": "ISC",
1212
"devDependencies": {
13-
"@jazzer.js/jest-runner": "^1.5.1",
14-
"@types/jest": "^29.5.3",
1513
"@types/node": "^20.4.5",
1614
"express": "^4.18.2",
17-
"ts-jest": "^29.1.1",
1815
"ts-node": "^10.9.1",
1916
"typescript": "^5.1.6"
2017
}

‎js_ts/nodejs-ts/utils.ts

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'fs';
1+
import fs,{PathLike} from 'fs';
22
import child_process from "child_process";
33

44

@@ -14,108 +14,92 @@ export class TODO {
1414
}
1515
}
1616

17+
const filePath: PathLike = './todo.json';
18+
1719
function fileIsPresent() {
18-
return fs.existsSync('./todo.json');
20+
return fs.existsSync(filePath);
1921
}
2022

2123

2224
function writeToFile(data: TODO[]) {
23-
try{
24-
fs.writeFileSync("./todo.json", JSON.stringify(data));
25-
}catch (e) {
26-
throw new Error()
27-
}
28-
25+
fs.writeFileSync(filePath, JSON.stringify(data));
2926
}
3027

3128
function readFromFile() {
32-
try {
33-
return fs.readFileSync('./todo.json');
34-
} catch (e) {
35-
throw new Error()
36-
}
29+
return JSON.parse(fs.readFileSync(filePath).toString());
3730
}
3831

3932
function createAndWrite(dataToWrite: TODO){
40-
let initial_arr: TODO[] = []
4133
dataToWrite.id = 1
42-
initial_arr.push(dataToWrite)
43-
writeToFile(initial_arr);
34+
writeToFile([dataToWrite]);
4435
}
4536

4637
export function deleteEntry(id: string) {
4738
try {
4839
if (fileIsPresent()) {
49-
consttemp = readFromFile();
50-
let content=JSON.parse(temp.toString());
51-
let_index: number=-1;
52-
content.forEach((element: TODO, index:number) => {
40+
letcontent: [] = readFromFile();
41+
let id_to_delete: number=-1;
42+
if(content.length>0){
43+
letindex: TODO=content.find((element: TODO, _index: number) => {
5344
if (String(element.id) === id) {
54-
_index = index;
45+
id_to_delete = _index
5546
}
56-
});
57-
if (_index >= 0) {
58-
content.splice(_index, 1);
59-
writeToFile(content);
60-
}else{
61-
thrownewError();
47+
})
48+
if (id_to_delete >= 0) {
49+
content.splice(id_to_delete, 1);
50+
writeToFile(content);
51+
returntrue
52+
}
6253
}
63-
} else {
64-
throw new Error();
6554
}
66-
6755
}catch (e) {
6856
return false;
6957
}
70-
return true;
7158
}
7259

7360

7461
export function addEntry(dataToWrite: TODO) {
7562
try{
7663
if (fileIsPresent()) {
77-
const data = readFromFile();
78-
let content = JSON.parse(data.toString());
64+
let content = readFromFile();
7965
if (content.length > 0) {
8066
let last_id = content[content.length - 1].id
8167
dataToWrite.id = last_id + 1
8268
content.push(dataToWrite);
8369
writeToFile(content);
70+
return true
8471
} else {
8572
createAndWrite(dataToWrite)
73+
return true
8674
}
8775
} else {
8876
createAndWrite(dataToWrite)
77+
return true
8978
}
9079
}catch (e) {
9180
return false
9281
}
93-
return true
9482
}
9583

9684
export function deleteFile() {
9785
try {
9886
if (fileIsPresent()) {
99-
fs.unlink('./todo.json', (err) => {
100-
if (err) throw new Error()
101-
})
87+
fs.unlink(filePath, () => {})
10288
return true;
10389
}
104-
return false;
90+
return false
10591
}catch (e) {
10692
return false;
10793
}
10894
}
10995
export function listFile() {
11096
if (fileIsPresent()) {
111-
// read the file
112-
const data = readFromFile();
113-
const content = JSON.parse(data.toString());
114-
let respond = "";
115-
content.forEach((element: TODO) => respond = respond + "Id: " + element.id + "\tTODO: " + element.todo + "\tDeadline: " + element.deadline + "\n");
116-
return[true, respond]
97+
const content = readFromFile();
98+
if (content.length > 0){
99+
return content.map((element: TODO) => `Id: ${element.id}\tTODO: ${element.todo}\tDeadline: ${element.deadline}`).join('\n');
100+
}
117101
}
118-
return [false,null]
102+
return null
119103
}
120104

121105
export function commandExecution(command: string, fn: (a: string) => void) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /