Onto incomplete project number...mmh... seems like i lost count. Today i'll setup the skeleton of an Api project i intend to work on that adds, retrieves and deletes habits. I'll be using Node.js and Express. Let's get started.
Run npm init
in the project folder, accept all defaults. That creates a package.json file
Install express npm install express
Create an app.js file and include express
const express = require('express');
Create an instance of express
const app = express();
Set up the port
const port = 4000;
Create a router
const router = express.Router():
Handle some routes and chain a get/post function.
router.route('/habits')
.get((req, res) => {
//Some random code to test with
const response = 'My api';
return res.json(response);
})
.post();
Use our router
app.use('/habittracker', router);
Set up the server to listen for requests
app.listen(port, () => {
console.log(`listening on Port ${port}`);
});
Installl nodemon : npm install nodemon
. Nodemon watches our files for changes and restarts our server. Update the scripts in the package.json with:
"start": " nodemon node app.js",
This allows us to use npm start
to start the server.
Let's start the server, head over to postman and send a get request
It's working!
There we have it, over to procrastination.
Another day down: Day 8!
Top comments (0)