Node.js | Preparing to Use Node.js
What Is Node.js?
Node.js is a server environment that can perform work with JavaScript. When people hear “server environment,” they often imagine a Web server where HTML files are published. When they hear “server-side development,” many think of providing scripts written in languages such as Perl or PHP, where accessing a public address executes the script.
Node.js is different from that style. Node.js begins with “creating a server.” With Perl or PHP, a separate server environment such as Apache HTTP Server or XAMPP exists, and you create Web programs as components placed on that server. In Node.js, you create the script that itself runs as the server program. In that script, you also define what happens when a certain address is accessed. This is very different from traditional server development.
It may sound difficult to create the server itself, but Node.js provides a proper server object, so preparing it is not extremely hard. Node.js became popular because it lets you develop both server-side and client-side code in one language, JavaScript, and because its event loop model scales better than older Web server styles when access increases.
Node.js used to feel like an enthusiast-only environment because it mainly required Linux and because there were few places to run it. Now Windows and Mac versions are available, and cloud services make it possible for anyone to build sites with Node.js.
Node.js Development Environment
Node.js is distributed from the following address.
Korean documentation is also available.
Download Node.js from:
https://nodejs.org/ko/download/
Choose the installer for your platform. After installation, use the command prompt or terminal to execute Node.js commands. Confirm installation by checking the versions of node and npm.
$ node -v
v20.0.0
$ npm -v
9.6.4
If both commands show versions correctly, the installation is complete.
Editors
Prepare a convenient text editor. Visual Studio Code is recommended for Mac and Windows.
https://code.visualstudio.com/
For Windows, Notepad++ is also useful.
https://notepad-plus-plus.org/
Sublime Text is another option for Mac and Windows.
Writing a Node.js Script
Using Node.js is simple: write a script and run a command. Node.js scripts use JavaScript, but they differ quite a bit from scripts used in Web pages. For now, focus on writing and running one.
var http = require('http');
var server = http.createServer();
server.on('request', doRequest);
server.listen(1234);
console.log('Server running!');
// Request processing
function doRequest(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World\n');
res.end();
}
Save this as sampleapp.js, for example in a folder named node.js-sample on the desktop. Open the command prompt or terminal, move to the folder, and run it.
$ cd Desktop/node.js-sample/
$ node sampleapp.js
Server running!
Open a browser and access:
http://127.0.0.1:1234/
If Hello World appears, Node.js is working. To stop the running script, press Ctrl+C in the command prompt or terminal.
Heroku Operating Environment
Once you know how to run Node.js locally, the next step is deploying it to a server. Ordinary rental servers usually cannot run Node.js programs, because the server must be able to execute Node.js itself. Cloud services are commonly used for this purpose. Examples include Heroku, OpenShift, and Cloud Foundry.
Here, we use Heroku.
Heroku Registration
Visit Heroku and create an account.
Click Sign Up, enter account information such as name and email address, and create a free account. A confirmation email arrives; click the link, set a password, and registration is complete. The Heroku Dashboard is the management screen for published Web applications.
Installing Heroku Toolbelt
Install the Heroku Toolbelt, which installs the programs needed to use Heroku, such as Ruby and Git.
Install it using the installer for your platform. After this, prepare the files needed for deployment.
Files Required for Heroku Deployment
To run a Node.js application on Heroku, you need package.json and Procfile, and you must slightly modify the Node.js script.
package.json is placed in the application root and contains application information in JSON format.
{
"name": "... application name ...",
"version": "version name"
}
Create a file named Procfile without an extension in the application root.
web : node sampleapp.js
This tells Heroku to run the specified script. Finally, change the script’s listen call.
Before:
server.listen(1234);
After:
server.listen(process.env.PORT, process.env.IP);
Now the files are ready for deployment. Deployment is performed from the command line with Heroku Toolbelt and Git. You may generate and register an SSH public key with:
$ ssh-keygen
$ heroku keys:add
Then move to the application directory, create the Heroku app, commit the files with Git, and push to Heroku. Once deployment completes, open the generated Heroku URL to confirm that the Node.js application is running.