One: Copy the Code of the official website, build a simple server
Const HTTP = require (' http '); const hostname = ' 127.0.0.1 '; const PORT = 3000;const Server = Http.createserver ((req, res) =& Gt { Res.statuscode = 200;//Return Status Code res.setheader (' Content-type ', ' text/plain '); The return type is plain text res.end (' Hello world\n ');}); Server.listen (port, hostname, () = { Console.log (' Server running at http://${hostname}:${port}/');});
II: Understand the Nodejs module
Three: Create a simple Campus module:
1: Add student's module: students.js
function Add (name) {Console.log (' Add student ' + name)}exports.add = add
2: Add Teacher's module: teacher.js
function Add (name) {console.log (' Add teacher ' + name)}exports.add = add
3: Add module of class: Klass.js
The module needs to be requirevar student = require ('./student ') //To assign the module object to the local variable var teacher = require ('./teacher ') function Add ( TeacherName, students) {Teacher.add (teachername) Students.foreach (function (item, index) {Student.add (item)})} Exports.add = Add
4: Add a school module: index.js
var Klass = require ('./klass ') Exports.add = function (klasses) {ForEach (function (item, index) {var _klass = Itemvar Teach Ername = Item.teachernamevar students = Item.studentsklass.add (TeacherName, Students)})}
Nodejs Basic Learning