node. JS Project Development Issue highlights (not regularly updated, at any time to the development process encountered problems added)

Source: Internet
Author: User

1. How to define common head and tail when developing site with Express
Scenario 1: Add an ASP-era-like include, such as a ejs template:

<% include: /header.ejs%>

Scenario 2: Using an MVC-like layout template, this installs a module:npm Install express-partials
Specifically how to apply, you can search: node. JS Express Layout
It's not going to be detailed here.


2, Windows, using WEBSTORM8, can not debug Express project, troubleshooting a half-day, some people in the team to debug, some people can not debug,
The phenomenon is click Run, can run the site, but click to Debug, in the Webstorm console window, the process will appear to automatically exit the prompt:

"C:\Program Files (x86) \jetbrains\webstorm 8.0.3\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe"--debug-brk=  54923--nolazy Bin\wwwdebugger listening on port 54923Process finished with exit code-1073741510 (0xc000013a:interrupted By CTRL + C)
Sweat, unexpectedly said is the interruption caused by CTRL + C ... According to this hint, Baidu and Google have been checked for half a day, a little news also did not,

Repeatedly troubleshooting items that can be debugged and items that cannot be debugged are finally discovered because:
The path where the item is located contains Chinese characters ...
Remember: developing a node. JS project, the path to the file, must not have Chinese characters

3, node. JS project files must be saved with UTF-8 encoding format , otherwise the contents of the Chinese characters, such as double-byte character, will be problematic, there are various inexplicable problems OH

4, node. JS development site, can only listen to the specified port, unlike IIS, does not support multi-Host header (multi-domain), if it is true to support multi-host header, only through the reverse proxy such as nginx, map to different ports node. JS Process

5, the JSON file does not support comments, any format comments are not supported, such as//* */<!----;
If you want to add a comment, you can only use a key-value pair scheme, such as: {"A": 123, "A means": "Just a Number"}

6. Node. JS does not have a Web. config file similar to ASP. NET, but it can be replaced with a JSON object, such as adding a settings.js file under the project root directory, as follows:

Module.exports = {    abc:123,    def: "Www.baidu.com",    mysql:{            Host: "192.168.189.100",            User: "Root" ,            database: "DbTest",            Port: "3306",            Password: ' 123 ',            connectionlimit:10,            charset: "UTF8"        }};
This is then called in other files:
var mysql = require (' mysql '); var settings = require ('.. /settings ');//Get def attribute, output console.log (SETTINGS.DEF);//Get MySQL connection information var pool  = Mysql.createpool (Settings.mysql);

7, express project, directly create and return the static HTML file
Scenario 1: Place the HTML file in the project's public directory, such as/public/abc/def.html
Then the request URL is: http://localhost:3000/abc/def.html
Scenario 2: After reading the file, output the string in the following code:
var Express  = require (' Express '); var path  = require (' path '); var router = Express. Router (); Router.get ('/', function (req, res) {    sendhtmlfile (res, "./index.js");}); Module.exports = Router;function Sendhtmlfile (res, relativepath) {    var htmlpath = Getabspath (relativepath);    Content-type Res.sendfile (Htmlpath) that automatically recognizes and outputs responses by extension    ; Convert relative path to absolute path function Getabspath (relativepath) {    var htmlpath = __dirname + "/" + RelativePath;    Return Path.resolve (Htmlpath);}



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.