Alrighttttt I am about to embark on some backend development for my Streams app. This document acts as a kind of guide for future development processes, as the exact processes are rather rusty in my mind.
I am starting out from completely no backend on my simple React app, with a couple of frontend components.
I think the first thing I need to do is to set up an express server.
My knowledge is rusty lol. Express is simply a library that offers a pleasing interface for backend development: i.e. setting up routes where you can offer raw data in the JSON format to the frontend.
The first thing to know, is that you will need to create another folder altogether to work on the backend, because the backend is a standalone application.
Steps for creating the backend folder
Step 1. Create a new backend folder.
In my case, I will create a folder called ‘streamsapp_backend’
Step 2: cd to that folder.
Step 3: run npm init
Answer the questions as follows:
| |
This will create a package.json file.
Step 4: Add "start": "node index.js" in the package.json file, like so:
| |
Step 5: Do a quick test to see if everything’s working:
- add
console.log('hello world')in index.js. - run
node index.jsfrom command line - or, run
npm start
Step 6: Install express into the backend
npm install express
Now, you can beginning coding your server
Step 7: Within index.js, import express and create an app.
| |
Step 7: Place some placeholder JSON data within a variable to begin working setting up routes
| |
Step 8: Create the routes below:
| |
The entire index.js file looks something like this:
| |
Important to note that you’re using another port for backend.
Step 9: Test the route to see if requests are going through.
npm startGo to localhost:3001/notes and see if the server delivers the JSON response.
Further Optimizations:
Step 10: Install nodemon
So that you won’t need to restart server whenever there’s changes to backend source code.
npm install --save-dev nodemon
Then, add the following to package.json:
"dev": "nodemon index.js",
under scripts.start.
To start server after this point, run npm run dev
Some references:
Getting single data
| |
Deleting resource
| |
Using Postman
Step 1 - Opening postman from laptop top bar.
Begin Sending requests.
That’s about it! You’ve set up your express backend server. Great.