Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CloudComputing_Act1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jesus Galaz Reyes
CloudComputing_Act1
Commits
f6137306
Commit
f6137306
authored
6 months ago
by
Jesus Galaz Reyes
Browse files
Options
Downloads
Plain Diff
Merge branch '3-set-up-server-and-api-routes' into 'main'
Adding server.js functionality and main operations for todos.js Closes
#3
See merge request
!3
parents
99f997e7
9b019758
No related branches found
No related tags found
1 merge request
!3
Adding server.js functionality and main operations for todos.js
Pipeline
#15544
passed
6 months ago
Stage: test
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
routes/todos.js
+33
-0
33 additions, 0 deletions
routes/todos.js
server.js
+15
-7
15 additions, 7 deletions
server.js
with
48 additions
and
7 deletions
routes/todos.js
+
33
−
0
View file @
f6137306
const
express
=
require
(
'
express
'
);
const
router
=
express
.
Router
();
const
Todo
=
require
(
'
../models/todo
'
);
// Get all TODOs for the logged-in user
router
.
get
(
'
/
'
,
async
(
req
,
res
)
=>
{
const
todos
=
await
Todo
.
find
({
userId
:
req
.
user
.
id
});
res
.
json
(
todos
);
});
// Add a new TODO
router
.
post
(
'
/
'
,
async
(
req
,
res
)
=>
{
const
{
description
}
=
req
.
body
;
const
todo
=
new
Todo
({
description
,
userId
:
req
.
user
.
id
});
await
todo
.
save
();
res
.
json
(
todo
);
});
// Mark TODO as done
router
.
put
(
'
/:id
'
,
async
(
req
,
res
)
=>
{
const
todo
=
await
Todo
.
findById
(
req
.
params
.
id
);
todo
.
isDone
=
true
;
await
todo
.
save
();
res
.
json
(
todo
);
});
// Delete a TODO
router
.
delete
(
'
/:id
'
,
async
(
req
,
res
)
=>
{
await
Todo
.
findByIdAndDelete
(
req
.
params
.
id
);
res
.
json
({
success
:
true
});
});
module
.
exports
=
router
;
This diff is collapsed.
Click to expand it.
server.js
+
15
−
7
View file @
f6137306
// server.js
const
express
=
require
(
'
express
'
);
const
path
=
require
(
'
path
'
);
const
connectDB
=
require
(
'
./mongodb
'
);
// Import the MongoDB connection
const
app
=
express
();
// Basic route for testing
app
.
get
(
'
/
'
,
(
req
,
res
)
=>
{
res
.
send
(
'
Server is running!
'
);
});
// Middleware
app
.
use
(
express
.
json
());
// Use express' built-in body-parser for JSON
app
.
use
(
express
.
static
(
path
.
join
(
__dirname
,
'
public
'
)));
// Serve static files
// Connect to MongoDB
connectDB
();
// Call the function to establish the database connection
// Routes
app
.
use
(
'
/api/todos
'
,
require
(
'
./routes/todos
'
));
app
.
use
(
'
/api/users
'
,
require
(
'
./routes/users
'
));
// Start the server
const
PORT
=
process
.
env
.
PORT
||
3000
;
app
.
listen
(
PORT
,
()
=>
{
console
.
log
(
`Server is running on port
${
PORT
}
`
);
});
app
.
listen
(
PORT
,
()
=>
console
.
log
(
`Server running on port
${
PORT
}
`
));
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment