欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

node模块

程序员文章站 2022-03-06 13:20:39
...

node 模块

1.核心模块:内置的,开箱即用

  1. http
  2. .createServer(function (request, response) {
  3. response.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
  4. response.end("<h2 style='color:red'>今天的天气真好!</h2>");
  5. })
  6. .listen(8081);
  7. console.log("Server running at http://127.0.0.1:8081/");
  1. http
  2. .createServer(function (request, response) {
  3. response.writeHead(200, { "Content-Type": "application/json" });
  4. response.end(`
  5. {
  6. "id":925,
  7. "user":"李同学",
  8. "qq邮箱":"123456789@qq.com"
  9. }
  10. `);
  11. })
  12. .listen(8081);
  13. console.log("Server running at http://127.0.0.1:8081/");
  1. const fs = require("fs");
  2. fs.readFile(__dirname + "/libin.txt", function (err, data) {
  3. if (err) return console.error(err);
  4. console.log(data.toString());
  5. });

2.文件模块:自定义的先声明,在导入

  1. module.exports = {
  2. domain: "WWW.php.cn",
  3. name: "PHP中文网",
  4. getSite() {
  5. return this.name + "(" + this.domain + ")";
  6. },
  7. };

let site = require("./m1.js");
3.第三方模块:npm安装的