Nodejs framework express4.2

Source: Internet
Author: User

Perface

Today I saw some nodejs, nodejs Development Guide. When I saw the expres, because the express version used in the book is different from mine, many functions cannot be implemented. Therefore, google Now records the process, bugs, and principles.

To playExpressNodejs will be installed. If your operating system is centos6.5, you can refer to my previous blog centos6.5 to install nodejs. Similar to other operating systems, see Node Installation.

Bug1 Express Command not found initial operation: Follow nodejs Development Guide npm install -g expressAfter the installation is complete, run the linux Command express --helpThe above reason is displayed. Solution: Install a package npm install -g express-generatorCause: express3 + has separated the function of creating an APP from express-generator, without it, you cannot create an Application 2 express-t ejs microblog. Instead of creating an ejs template engine, you can use the jade template engine for initial operations. express -t ejs microblogIs used to create an application. The application name is microblog and the template engine is ejs. However, you can view the package in the microblog folder. in json, we know that the created template is not an ejs template, but a jade template engine solution: the version is different. If you use the wrong command, it should be express -e microblog(-E is the ejs template) 3 node app. js did not work. Initial operation: Follow the node js Development Guide and type the node app on the command line. js, and then access the listening port 3000 in the browser. The problem is: the page cannot be accessed. solution: the version is different. If the command is incorrect, it should be npm startSummary

In fact, the steps for getting started with the express ejs template are as follows:

Npm install-g express & npm install-g express-generatorexpress-e microblog cd microblog & make install npm start Web browser access localhost: 3000 successful page is as follows:

For more information, see the express help documentation Express Guide.

Theory

In fact, I also encountered a bug later. This is not a version issue, but when I re-open a terminal in linux to input commands, the current directory is/home/monkindey/Desktop, which is actually on the Desktop.npm start"Error: ENOENT, open"/home/monkindey/Desktop/package. json '". At that time, I thought it was necessary to switch to the express application directory. Indeed, when I switched to the application directory, I ran the npm start command. Then I am confused.

First, why do I need to switch to the application directory to run npm start? Second, why does my npm start actually run the application file?

Let me give you a brief description.

Npm is a node. js package management tool, you can use it to download packages, view files, and other functions. The application created with express is a nodejs package that complies with CommonJS specifications. The package of the current directory will be read when npm executes the node. js package. json file, which is the cause of the above bug. Executing npm start is actually executing package. the command line corresponding to the start attribute in the object corresponding to the script in json. below is the package of my application. json:

Therefore, if you change the start value in package. json to monkindey or another string, and then you press npm monkindey/or another on the linux Command Line, the program will still run.

In fact, package. json is a configuration file, which is only in xml format. But in nodejs, json can be used, which is easy to understand. From package. json. We can see that npm start actually runs. /bin/www is to create a server and listen to port 3000, so we can access the application by entering "localhost: 3000" in the browser.

ReferenceExpress command not found
Node. js express npm start
Express Getting started
Nodejs Development Guide

Related Article

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.