forked from gitea_admin/simple-node
backend files
This commit is contained in:
41
backend/index.js
Normal file
41
backend/index.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3001;
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Todo API is running. Access todos at /api/todos');
|
||||
});
|
||||
|
||||
let todos = [
|
||||
{ id: 1, task: 'Learn Node.js', completed: false },
|
||||
{ id: 2, task: 'Build a microservice app', completed: false }
|
||||
];
|
||||
|
||||
app.get('/api/todos', (req, res) => {
|
||||
res.json(todos);
|
||||
});
|
||||
|
||||
app.post('/api/todos', (req, res) => {
|
||||
if (!req.body.task) {
|
||||
return res.status(400).json({ error: 'Task is required' });
|
||||
}
|
||||
const newTodo = {
|
||||
id: Date.now(),
|
||||
task: req.body.task,
|
||||
completed: false
|
||||
};
|
||||
todos.push(newTodo);
|
||||
res.status(201).json(newTodo);
|
||||
});
|
||||
|
||||
if (require.main === module) {
|
||||
app.listen(port, () => {
|
||||
console.log(`Backend service listening at http://localhost:${port}`);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = app;
|
||||
Reference in New Issue
Block a user