INFO PAGE
NodeJS
NodeJS is an asynchronous JavaScript runtime for building scalable network applications. It is JavaScript on the server and actually the server as well. Node also has Node Package Manager (NPM) that lets developers distribute and use apps.
- BEFORE installing Node, make sure your computer updates are up-to-date.
- Download Node at https://nodejs.org.
- Close all applications including the browser and then install Node.
- Follow all the defaults and check Automatically install the necessary tools.
- Once installed, open a Command Prompt from the Start Menu search
// Type the following and press Enter to see the version of Node node -v // Enter the following to see the version of Node Package Manager npm -v
Node Package Manager (NPM)
Packages are how node files are organized and shared. See https://npmjs.com. Packages usually rely (depend) on more pacakages. Packages always have a package.json file that holds configuration data such as the following:
{ "name": "zimjs", "version": "16.2.5", "type": "module", "main": "./src/zim.js", "types": "./ts-src/typings/zim", "scripts": {}, "devDependencies": { "typescript": "^4.5.2" }, "dependencies": { "@zimjs/createjs": "^1.4.1" }, "repository": { "type": "git", "url": "https://github.com/danzen/zimjs.git" }, "keywords": [ "ZIM", "ZIMjs", "Canvas", "Components" ], "author": "Dr Abstract - Dan Zen", "license": "ISC" }
PROJECT
Let's make a project:
- Make a folder called firstnode in your Web Programming directory
- Drag this folder onto your VS Code desktop icon (or open it)
- Just make sure that this is the only folder in your VS Code.
- Add an app.js file with the following code:
- Open a terminal in VS Code (CTRL `) or use Terminal menu
- I open a terminal by dragging the bottom of VS Code up
- Type the following into the terminal:
- Note the package.json file that gets made
- To run the app type the following:
- Open a browser and type http://localhost:3000 in the browser bar
- You should see Hello world! in the browser
- To make changes to your app you must stop it running in the terminal
- Press CTRL C to stop
- Constantly stopping and starting after each update is annoying
- We will install a package called nodemon to help us
- Note that a node_modules folder with content has been installed
- Add the following in the scripts brackets {} in the package.json file
- "start": "nodemon app.js"
- Make sure to put a comma after the test line that is in there
- Now start the app using the following:
- Each time you save your app.js file nodemon will restart the app
- Not so important for this app, but handy when actually coding
// Load HTTP module const http = require("http"); const hostname = "localhost"; const port = 3000; // Create HTTP server (in future, we do this with the Express package) // req - will hold a request object (what is coming in) // res - will hold a response object (what is going out) const server = http.createServer((req, res)=>{ // Set the response HTTP header with HTTP status and Content type res.writeHead(200, {"Content-Type": "text/plain"}); // Send the response body "Hello world" res.end("Hello world\n"); }); // Start the server listening server.listen(port, hostname, ()=>{ console.log("Server running on port " + port); });
// type the following and press ENTER and answer the questions npm init // Package name: the default is fine, change the name if you want // Version: the default is fine // Description: my first node.js program then press ENTER // Entry point: this should default "app.js" // Test Command: leave blank // Git Repository: leave blank // Keywords: can leave blank // Author: your first and last name // License: accept default
// type the following and press ENTER (no need for .js) node app // you should see Server running on port 3000 in the terminal
INSTALLING A PACKAGE
// type the following and press ENTER. npm install -D nodemon // you can use i instead of install // -D will save a dev dependency (not for distribution) // see the update in your package.json // TERMINAL TIPS // to copy code from the lessons page or somewhere else // select the code and use CTRL C // then right click in the terminal - it will add the code // you can use the arrow up and down // to go to a previous and next commands
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "nodemon app.js" },
// type the following and press ENTER. npm start // this calls the start script which runs the app with nodemon
CONCLUSION
We have installed NodeJS and gone through the basics for creating and running a node app. In other pages we will install more packages including an important package called Express which helps us server our pages using an important concept called Routing.
FULL Sheridan Node documentation can be found in Wendi Jollymore's Lessons.