fetch api,npm 与 node 模块的使用
一.fetch_api,async,await
let url = "https://jsonplaceholder.typicode.com/todos/1";
//fetch api
fetch(url)
.then((response) => response.json())
.then((json) => console.log(json))
.catch((err) => console.log("error"));
//ecma2017,async await 简化fetch
async function getUser(url) {
try {
const response = await fetch(url);
const result = await response.json();
console.log(result);
} catch (error) {
console.log("error");
}
}
getUser(url);
二.npm 安装与删除包的常用操作
npm i axios :全部安装
npm i axios -S:安装生产依赖
npm i axios -D:安装开发依赖
npm ui axios:删除包
三.node 模块
//核心模块
const http = require("http");
console.log(http.Agent);
//文件模块
const site = require("./outside.js");
console.log(site.getSite());
//第三方模块: npm安装的
let site = "php中文网";
let getSite = function () {
return site + " (php.cn)";
};
exports.site = site;
exports.getSite = getSite;