A super simple web app with Node.js and the Express web framework
- A browser (e.g., Chrome)
- A text editor (e.g., VS Code, or Notepad++, or Chrome)
- Folder-based
- Easy to write
- Node.js non-blocking event loop supports many concurrent requests
- Express framework for web apps offers concise, easy-to-use API
- Built-in objects for application, request, response, with HTTP verb methods
- Node.js
- npm Node Package Manager
Note that a dependency on Express.js has been added to package.json.
Install the dependencies listed in package.json:
- Open PowerShell here as admin and run:
npm install
. - Verify node_modules was created.
- Review the contents of node_modules.
Open PowerShell here as admin and start your app with node: node app.js
- Open a web browser. Try these URLs
- http://127.0.0.1:3002/ or http://localhost:3002/.
- http://localhost:3002/hello
- http://localhost:3002/big
- http://localhost:3002/greeting/42
- http://localhost:3002/yo/Lohita
- http://localhost:3002/yo/Rahul
- http://localhost:3002/yo/Teja
- Make changes to the server logic.
- Use CTRL-C, CTRL-C to stop your server.
- Restart the server to see your changes.
- Install nodemon globally - this is one of the rare packages to install globally.
- In your folder, open PowerShell here as Administrator, install and check the version. Then start the app with nodemon (now we don't have to restart after code changes.)
npm install nodemon -g
nodemon -v
nodemon app.js
- Open PowerShell as Admin, run ipconfig.
- Find your IPv4 address.
- Invite others to interact with your server-side app.
We don't want to commit the auto-generated node_modules - and other things like developer-specific contents, local history, etc.
List all files and folders for git to ignore (not commit and push to the repo) to a file in the root folder named: .gitignore
- Create a folder for the app.
- In the folder, create a file named app.js.
- In the folder, open PowerShell and run
npm init
to generate a package.json with app information. - Add README.md.
- Add .gitignore.
To create a file without a name, there are 3 common options:
- In Windows File Explorer, create ".gitignore." (Type a dot at beginning and at the end. It will drop the second dot.
- In Git Bash:
touch .gitignore
- In PowerShell:
ni .gitignore
If you get:
Error: listen EADDRINUSE: address already in use 127.0.0.1:3002
There is already an app running on the port. Hit CTRL+ALT+DELETE to see the tasks, and find the running app and Right-click / End Task to kill the current version.
Then restart the app as explained above.
- Node.js platform
- npm (Node package manager)
- nodemon
- npm install {packagename}
- npm install -g {packagename}
- npm start
- package.json
- .gitignore
- localhost (127.0.0.1)
- host
- port
- URL
- route
- web server
- web service
- web client
- web request
- web response