批改状态:合格
老师批语:
/// 1. 核心模块, 无须声明,直接导入并使用const http = require("http");// console.log(http)const fs = require("fs");// console.log(fs)// 2. 文件模块: 先声明,导出, 再导入;导入必须添加文件路径// exports// 推荐方案,一次性导出module.exports = {domain: "help10086.cn",name: "PHP中文网",getSite() {return this.name + "(**" + this.domain + "**)";},};// 导入let site = require("./m1.js");// console.log(site)// console.log(site.getSite())site = require("./m2.js");console.log(site);console.log(site.getSite());
| STT | api 方法 | 说明 |
|---|---|---|
| 1 | response.writeHead | 设置响应头 |
| 2 | response.end | 写入并结束 |
// http模块// 导入http模块const http = require("http");// 创建一个web服务器// request 请求对象// response 相应对象http.createServer((request, response) => {// 设置响应头// response.writeHead(200, { "Content-Type": "text/plain" })// 写入并结束// response.end("Hello Help")// html// "text/plain" -> "text/html"// response.writeHead(200, { "Content-Type": "text/html" })// response.end("<h1 style='color:red'>Hello Help</h1>")// json// "text/html" -> "application/json"response.writeHead(200, { "Content-Type": "application/json" });response.end(`{"msnv":900101,"name": "minh","email": "minh@gmail.com"}`);}).listen(8081, () => console.log("http://127.0.0.1:8081/"));
// 文件模块// console.log(__dirname)const fs = require("fs");fs.readFile(__dirname + "/readme.txt", (err, data) => {if (err) return console.error(err);console.log(data.toString());});
// path模块const str = "./node/hello.js";const path = require("path");// 绝对路径console.log(path.resolve(str));// 目录部分console.log(path.dirname(str));// 文件名console.log(path.basename(str));// 扩展名console.log(path.extname(str));// pathStr -> objconsole.log(path.parse(str));// obj->pathStrconst obj = {root: "",dir: "./demo",base: "test.js",ext: ".js",name: "test",};console.log(path.format(obj));
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号