I have just come into contact with node.js and have recorded some processes and have been reviewed in the future. If there are any uncertainties or mistakes, criticism and correction are welcome.
What is Node.js?
I read some articles on the Internet and my understanding is that the function is similar to apache, which can be understood as server-side. However, the implementation mechanism is different, and the concurrency effect is very good. Its goal is to replace the Apache server mechanism.
OK, let’s start the environment configuration:
1. Download Node.js
Go directly to the official website to download it, and select Windows Installer (.msi) version 64 bit. Here you will find a Windows Binary (.exe), which is an independent Node.js environment terminal. You don’t need to install it after downloading it, so you can use it directly. I suggest downloading .msi and installing it.
2. Install Node.js
Because it is a Win8 system, some problems will arise during installation.
1) Error 2502, Error 2503
When you see this kind of problem, everyone knows that it is because the permissions are insufficient, so just use the administrator's permission to execute it.
2) Unable to execute .msi file
You can right-click cmd, open the terminal as an administrator, and then execute the "msiexec /package node-v0.10.31-x64.msi" installation. It will be OK all the way.
3) How to verify that the installation is successful
cmd enters the Node.js installation directory. For example, mine is "C:/cc/nodejs". In this directory, you will see several executable files such as node.exe, npm, etc. If you have added the installation path to the Path, you do not need to enter the installation directory to execute node.
Enter node -v into cmd to view the version;
You can also enter Node mode and enter "console.log("Hello world!");" to see if the output is normal, as shown in the figure below:
4) Further verify Server functions
Create a new js file, such as test.js, with the following content:
var http = require("http");http.createServer(function(req, res) { res.writeHead(200, {"Content-Type":"text/html"}); res.write("<h1>Node.js</h1>"); res.write("<p>Hello World</p>"); res.end("<p>This is just testing Node working !!! </p>");}).listen(3000);console.log("HTTP server is listening at port 3000.");Then execute: node test.js on the command line, as shown in the following figure:
This is to open http://127.0.0.1:3000/ in the browser to see the output web results:
OK, if everything is normal at this point, it means that the basic function of Node.js has been installed successfully!
But we often need to use some other installation packages, such as express, so let's talk about npm next
3. Install npm module
First check the npm config configuration status: enter the command npm config list
Here are a few points of explanation, which are also places where Win8 or Chinese users need to understand and configure on demand:
1) Create a new directory npm in the C:/Users/***/AppData/Roaming/ directory, otherwise an error will be reported when executing npm install.
2) You can run the following two commands to set up the proxy, and pay attention to changing the proxy address to the one you actually available.
npm config set proxy=http://127.0.0.1:8087 (this is the default) npm config set proxy=null (this is set to not use proxy)npm config set registry=http://registry.npmjs.org
3) If you get here step by step and report an error, it is usually an agent problem.
4) If successful, it should be OK to execute npm install expression at this time.
4. Install other required modules, just npm install name. Let me mention it here that npm supports installing modules defined by itself. But beginners don't need to worry about this.
OK, the Node.js configuration is completed and the work is over.