node 导入与导出

一 node与javascript的导入与导出

1 导出当前模块的内容

javascript 复制代码
//导出单个值
module.exports = function add(a,, b) {return a + b;}

//导出多个值
module.exports = {add, subtract};

//逐个导出
exports.add = function(a, b) {return a + b;};
exports.subtract = function(a, b) {return a - b;};

2 导入

javascript 复制代码
const math = require('./math')     //引入本地文件
const path = require('path')    // 引入核心模块
const express =  require('express')    //引入第三方模块

3 javascript中的export快捷导出

javascript 复制代码
// 命名导出
export const name = 'Alice'
export function greet() {return 'hi'}

// 默认导出
export default class Person {}

// 同时导出
export default class Person {}
export const age = 25

4 javascript中的导入

javascript 复制代码
import Person, {name, greet} from './person.js';
import * as utils from './utils.js';
相关推荐
BreezeJiang20 小时前
别把 WebSocket 当成一门新协议学:搞懂"借 HTTP 握手",双端 Demo 和跨域就都通了
websocket·node.js
65岁退休Coder1 天前
把 Agent 框架拆开:PI 开发生产级 Harness
后端·node.js·agent
65岁退休Coder2 天前
PI Agent 开发一个生产级 Harness
后端·node.js·agent
szephyr2 天前
WebSocket 实战:心跳、断线重连、鉴权,一次讲清
前端·websocket·node.js·长连接·实时通信
秋秋小事2 天前
node postgreSQL的select与include
node.js
ID34610744202 天前
【课程设计】基于Spring Boot+Vue的游戏账号租赁系统的设计与实现-计算机毕设 附源码50345
javascript·vue.js·spring boot·python·node.js·php·课程设计
柚yuzumi2 天前
彻底搞懂 JavaScript 类型转换:显式转换、隐式转换与 ToPrimitive
前端·javascript·node.js
cpolar技术支持2 天前
本地登录正常,公网却掉线?Express Session 经 cpolar HTTPS 访问的 Cookie 排障实战
node.js·express·cpolar·cookie·session
秋秋小事2 天前
node prisma+postgreSQL数据库事务
node.js
badhope2 天前
C++、Node.js、Python 写最小 HTTP 服务谁快?我实测了 6 万次请求
node.js