Nodejs first writing experiences and nodejs writing experiences
How to view and install other tools after nodejs Installation
There are already a lot of articles on nodejs on the Internet. Here I just write down my little experiences. It would be better if I could help others.
I will not describe the installation of nodejs here. Just download it from the official node. js website. It is recommended that you download the installation version directly without compiling it yourself. The nodejs installation version is the same as installing other software.
Versions later than nodejs 0.6.0 can be run in windows and the npm package installation manager is provided.
After nodejs is installed, Start node. js will open a black-like system command box. Here is the command box for directly inputting js Code. Therefore, if you enter node-v here, you will be prompted that you do not have the node command, such:
If you want to install other packages, such as express, you need to find Node. js command prompt in the Start Menu or directly run cmd to enter the command line.
After entering the command line, enter node-v to view your installed nodejs version. Enter node-h to view the help of nodejs.
You can also enter npm-v to check whether the npm nodejs package manager has been integrated.
Nodejs's first demo
Create a js file (for example, server. js) in any directory on your computer)
123456789 |
// Request http Module var http = require( "http" ); http.createServer( function (req,res){ res.writeHead(200,{ "Content-Type" : "text/html" }); res.write( "); res.end( "<p>Hello World2</p>" ); }).listen(3000); console.log( "Http server is listening at port 3000" ); |
Open the node command line, enter the directory of the server. js file you created, and enter the node server. js command to run the server File.
Enter http: // localhost: 3000 in the browser and press Enter,
If you see the above page, congratulations, your first demo has been successfully executed.
Nodejs debugging
Nodejs debugging is more difficult than php, python, and other background languages.
You will get used to directly refresh the browser after modifying the PHP script to observe the results, and you are developing Node. the HTTP application implemented by js will find that no matter which part of the code you modified, Node must be terminated. it will take effect after js is re-run. This is because Node. js parses the script file only when it is referenced to a part for the first time. In the future, it will directly access the memory to avoid repeated loading, PHP always reads and parses the script again (if there is no special Optimization Configuration ). Although this design of Node. js is conducive to improving performance, it is not conducive to development and debugging, because we always want to see the effect immediately after modification during the development process, rather than terminating the process and restarting each time.
Supervisor can help you implement this function. It will monitor your code changes and automatically restart Node. js.
The usage is simple. First, use npm to install the supervisor:
1 |
$ npm install -g supervisor |
If you are using Linux or Mac, directly typing the above command may cause a permission error. The reason is that npm needs to install the supervisor to the system directory and requires administrator authorization. You can use the sudo npm install-g supervisor command to install the supervisor.
Next, run the supervisor command to start app. js:
It is only a js file in the current directory, or a server script. When you change this file, nodejs will be restarted.