1. Installation:
Please refer to: http://www.runoob.com/nodejs/nodejs-install-setup.html to download and install
2. The module:
With Node.js, all functionality is broken down into modules and a complete module loading mechanism is provided, so we can divide the application into different parts. It is impossible to write the entire business in a single JS file. You have to have MVC.Copy the code
In a narrow sense, each JavaScript file is a module; Multiple JavaScript files can require each other, they implement a function together, they collectively external, also known as a broad module
In Node.js, variables and functions defined in a JavaScript file are only valid within that file. When you need to refer to these variables or functions outside of the JS file, you must expose them using exports objects. The user references the JS file with the require() command.
For example: code in the test.js file:
Var MSG = "hello "; exports.msg = msg; MSG is a variable that is only scoped inside a JS file.Copy the code
If someone else wanted to use this variable, they would use exports to expose it.
User:
var test = require("./test.js");
console.log(test);
Copy the code
The user receives the exports object with test, that is, the test variable is the exports variable in the file.
A JavaScript file that can export countless variables and functions. But when require, you only need to require the JS file once. Use dot syntax when using its variables and functions. So, invisibly, a top-level namespace is added.
Node, js files and JS files are built into a network of exports and require. It’s not held together by HTML files.
Note that in the require command, we would write:
var test= require("test.js"); // No dot /, so it's not a relative path. It's a special path. Node.js treats this file as a file in node_modulesCopy the code
The node_modules folder does not have to be in the parent directory, it can be in any direct ancestor directory. You can even put it in the folder of the NODE_PATH environment variable. The advantage of this, as you’ll see later: you don’t have to bring modules with you when sharing projects.
We can use folders to manage modules, such as var bar = require(“bar”); Node.js will then look for index.js in the bar folder under node_modules to execute.
For each module folder, it is recommended to write a package.json file with an unchangeable name. Node automatically reads the configuration. There is a main entry, which is the entry file:
{“name”: “jinsanpang”, “version”: “1.0.1”, “main”: “app.js”} This package.json file is put in the root of the module folder
Based on what we have learned above, we know that a module is the encapsulation of some functions, so some mature and frequently used functions are encapsulated into modules. And put it in the community for free download. The great tool is NPM (https://www.npmjs.com/). You can use the NPM install module name to install your own modules
Note that when require() another JS file, that js file will be executed. The path in require() starts from the current JS file and finds someone else. Fs finds people from the command prompt.
3. Template Engine:
Personally, I would prefer to have the front and back ends separated for us front-end developers. If you're a backend developer, you can use a template engine to implement a front-end view. There are two famous ones: ejS and Jade. We are interested can refer to: http://blog.csdn.net/show_me_the_world/article/details/51945035 and https://www.jianshu.com/p/e2a9cd3b7e56Copy the code
This series of courses is based on practical examples, because of my limited knowledge, if you have any questions during the process, please leave a message to me.
Next section: Node Hands-on Express Framework. Welcome to continue learning with me