Notes on rapid development of simple backend API server
Last updated on 7 Mar 2024
Note: This post is a details-post for the post: Learning web app development through free online tutorials – Organized Notes and Log, https://raviswdev.blogspot.com/2024/03/learning-web-app-development-through.html .
I want to know if I can very quickly create a simple backend API server for posts used in my code variation of the React tutorial blog app and connect the blog app deployed on netlify to the backend. A simple database would have to be used to store the posts data.
This post captures the info. I am gathering about the above.
In case, node.js is not installed, download and install the LTS version: Node.js download.
From Express Getting Started :
For bare-bones starter project:
- Create empty directory
- Run 'npm init' accepting defaults except for entry point which may be changed to app.js. This creates package.json file which can be modified later on, if default values used by npm init have to be changed.
- If express is not installed, run 'npm install express'
- Create app.js empty file. Copy-paste Hello world example code into it.
- Run app.js using 'node app.js' command.
- Load http://localhost:3000/ in a browser to see the bare-bones server output.
For Express Application Generator application skeleton:
- Create empty directory
- Run 'npx express-generator'
- Run 'npm install'
- An issue is that using the npx command message specified run command for generated application (e.g. 'SET DEBUG=ravi-test-exp-gen:* & npm start') gives an error in VS Code terminal. Changing it to the Windows Powershell command format provided in above Express Application Generator page fixes the issue (e.g. '$env:DEBUG='ravi-test-exp-gen:*'; npm start'. On successful running of the app, it shows a server listening message on console (e.g. 'ravi-test-exp-gen:server Listening on port 4000 +0ms') ... Just running 'npm start' also starts the app sucessfully.
- Load http://localhost:4000/ in a browser to see the Express generator application skeleton output ('Welcome to Express').
- By default, 'jade' view engine files are created. I don't know anything about it. The above web page provides a list of options for express -h command which include view options. I am not sure how these options can be specified in the npx express-generator command.
- Notable features of the default application skeleton are/seem to be: routes, views, morgan logger, static (public directory) and server logging for listen and any associated errors
--------------
TOP 5 Node-Express Boilerplates For Building RESTful API's In 2021. Had a quick look. I think the code for all the boilerplates is significantly more complex than the tutorial code I have been exposed to, so far. They seem to be exhaustive but too complex for my current needs. May be good for some of my future needs though.
--------------
For my current needs, I think I will use appropriate node express tutorial code as a starter base and modify that to suit my needs. Later on, I think I need to explore some of the above mentioned generator(s) and boilerplates.
Comments
Post a Comment