init
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const bodyParser = require('body-parser');
|
||||||
|
const cors = require('cors');
|
||||||
|
const app = express();
|
||||||
|
const port = 5000;
|
||||||
|
|
||||||
|
app.use(cors());
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
let notes = [];
|
||||||
|
|
||||||
|
app.get('/api/notes', (req, res) => {
|
||||||
|
res.json(notes);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/api/notes', (req, res) => {
|
||||||
|
const newNote = req.body.note;
|
||||||
|
if (newNote.trim() !== '') {
|
||||||
|
notes.push(newNote);
|
||||||
|
res.json({
|
||||||
|
success: true
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.json({
|
||||||
|
success: false,
|
||||||
|
error: 'Note cannot be empty'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.delete('/api/notes/:index', (req, res) => {
|
||||||
|
const index = parseInt(req.params.index);
|
||||||
|
if (index >= 0 && index < notes.length) {
|
||||||
|
notes.splice(index, 1);
|
||||||
|
res.json({
|
||||||
|
success: true
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.json({
|
||||||
|
success: false,
|
||||||
|
error: 'Invalid index'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log(`Server is running on port ${port}`);
|
||||||
|
});
|
||||||
Generated
+1234
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "notes-backend",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node index.js",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"body-parser": "^1.20.2",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"express": "^4.18.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user