Node. JS's first demo and local server configuration

Source: Internet
Author: User
Tags php script node server

Installation Nodejs Here is not described, directly on the NODEJS official website download is good, beginners are recommended to download the installation version directly, do not have to compile their own. Nodejs installs and installs other software like, no threshold. Starting node. js after installing Nodejs will open a black-like system command box, here is the command box to enter the JS code directly, so enter here Node-v will prompt you to not have node this command, such as: If you want to install other packages, such as Express, etc., You will need to find the node. js command Prompt in the Start menu or run cmd directly into the command line. After entering the command line, enter NODE-V to see the version of the Nodejs you have installed. Enter Node-h to view Nodejs's help. You can also enter NPM-V to see if the NPM Nodejs Package Manager is already integrated. Nodejs's first demoCreate a JS file in any directory on your computer (ex: server.js)-Vim server.js//New JS file
// 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(console.log); ("Http server is listening at Port 3000");

Open the node command line, enter the directory where you created the Server.js file, and type the node Server.js command to run the server file. Then enter in the browser: http://localhost:3000 enter. (If the local server is not turned on, see next to open the local server) if you see this page above, then congratulations, the first demo has been successfully executed. turn on the local Apache server
Take Mac For example, create a new Sites folder in the root directory and put some dummy data for demo or test. Locate the configuration file and back up the original file.
// Switch working directory $cd/etc/apache2//   backup files, safekeeping, only need to be executed once $sudo CP httpd.conf Httpd.conf.bak

If you are prompted for an error, you need to restore the backup httpd.conf file, execute the following code

$sudo CP Httpd.conf.bak httpd.conf
Modifying a configuration file
// edit httpd.conf with vim $sudo vim httpd.conf // Find DocumentRoot   /documentroot

Press I to enter edit mode

You can see that there are two paths to change them to the path of the Sites folder you just built.

( If there is a 10.10 system, there is one more step: Find the options input/options can also visually find the position in the image itself, add a word between Options and follow, and then press the ESC key to exit edit mode, and then enter: Wq Save and exit if you don't want to save the wrong number: q!
// Copy the configuration file $sudo the CP php.ini. default php.ini // after restarting the Apache server, the following statement is normal $sudo apachectl-k restart

Confirm the success in the end no, just enter localhost in the browser to see the file directory under the Sites folder.

Note:

Commands for server switches

$sudo Apachectl-k stop
Nodejs CommissioningNodejs debugging is more difficult than some background language such as Php,python. In general, we'll be accustomed to refresh the browser directly after modifying the PHP script to observe the results, and in developing the node. JS implementation of the HTTP application, it will be found that no matter which part of the code you modify, you have to terminate node. JS and re-run to be effective. This is because node. js only parses the script file the first time it is referenced to a part, and then accesses the memory directly, avoiding repeated loading, while PHP always re-reads and parses the script (if there is no specific optimization configuration). This design of node. JS, while improving performance, is not conducive to development debugging because we always want to see the effect as soon as we change it, rather than terminating the process and restarting it every time. Supervisor can help implement this feature by monitoring changes to the code and automatically restarting node. js. The easy way to use it is to first use NPM to install supervisor:1$ NPM install-g Supervisor If you are using Linux or MAC, typing the above command directly is likely to have a permissions error. The reason is that NPM needs to install supervisor to the system directory, which requires administrator authorization and can be installed using the sudo npm install-g Supervisor command. Next, use the Supervisor command to start the app.js:1$ supervisor App.js is only the current directory of the JS file, also can be said to be a server-side script, when changing this file, will restart Nodejs.

Node. JS's first demo and local server configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.