Small Node.js sample project backed by PostgreSQL.
Endpoints:
POST /loginGET /user/:userIdPATCH /user/:userId
- Create a PostgreSQL database.
- Copy
.env.examplevalues into your shell or environment. - Install dependencies:
npm install- Start the server:
npm startThe server starts on http://localhost:3000.
Run the Jest test suite:
npm testRun the ESLint checks:
npm run lintBoth commands currently include intentional failures for CI testing.
On first start, the app creates the users table automatically and seeds two users:
demo/demonoah/demo
Use either the individual PG* variables or a single DATABASE_URL.
Example:
export PGHOST=localhost
export PGPORT=5432
export PGUSER=postgres
export PGPASSWORD=postgres
export PGDATABASE=sample_node_apiValidates credentials against PostgreSQL and returns a JWT plus the matching user ID.
Example:
curl -X POST http://localhost:3000/login \
-H "Content-Type: application/json" \
-d '{"username":"demo","password":"demo"}'Sample response:
{
"message": "Login successful",
"token": "<jwt-token>",
"userId": "user-101"
}Requires a bearer token and returns user details from PostgreSQL.
Example:
curl http://localhost:3000/user/user-101 \
-H "Authorization: Bearer <jwt-token>"Sample response:
{
"userId": "user-101",
"firstName": "Ava",
"lastName": "Sharma",
"email": "[email protected]"
}Requires a bearer token and updates user fields in PostgreSQL.
Example:
curl -X PATCH http://localhost:3000/user/user-101 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <jwt-token>" \
-d '{"firstName":"Maya","email":"[email protected]"}'Sample response:
{
"message": "User updated successfully",
"user": {
"userId": "user-101",
"firstName": "Maya",
"lastName": "Sharma",
"email": "[email protected]"
}
}