当前位置:网站首页>Nodejs 安装与介绍
Nodejs 安装与介绍
2022-06-30 17:46:00 【John是橘红】
Node.js 安装与介绍
1. 用 nvm 管理 node 版本
一般学习的时候,一个版本的 node 已经够用了,直接在官网上下载即可。但是在生产过程中,难免会遇见 node 为不同版本的项目,因此下载 nvm 是挺有必要的事情。如果是 windows 用户,请使用 nvm-windows!
https://github.com/nvm-sh/nvm/blob/master/README.md
安装:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
添加相关配置,nvm 默认位置是 ~/.nvm。
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
上面两行运行完后,通过指令 nvm list 可以查看 mvn 安装的 node 版本列表。如果安装成功后便可以显示。

安装指定 node 版本:例如要安装大版本为 16 的,直接 nvm install 16 即可。小版本一样的 nvm install 16.15.0。
切换其他版本 node:切换大版本号直接 nvm use 14,即可切换到大版本为 14 的 node 环境,指定小版本同理。
查看当前使用的 node 版本:node -v
2. nodejs 和 js 的区别
- ECMAScript(ES):定义了语法,写 JS 和 nodejs 都必须遵守
- 变量定义,循环,判断,函数
- 原型和原型链,作用域和闭包、异步
- 不能操作 DOM,不能监听 click 时间,不能发送 Ajax 请求
- 不能处理 http 请求,不能操作文件
- 因此,只有 ES,几乎做不了任何实际的项目
- JavaScript(JS):使用 ES 语法规范,外加 Web API,缺一不可。Web API 标准由 W3C 规定。
- DOM 操作,BOM 操作,时间绑定,Ajax 等
- 两者结合,即可完成浏览器端的任何操作
- nodejs:使用 ES 语法规范,外加 nodejs API。
- 处理 http,处理文件等
- 两者结合,即可完成 server 端的任何操作
总结
- ECMAScript 是语法规范
- nodejs = ECMAScript + nodejs API
- JS = ECMAScript + WebAPI
运行 nodejs 方法
命令行运行:
node js文件路径
即可,也可用 vscode 里的插件 Code Runner,一键运行,省的自己手输命令行。

3. commonjs
commonjs 导入导出
// a.js
function add(a, b) {
return a + b;
}
function mul(a, b) {
return a * b;
}
module.exports = {
add,
mul,
};
// b.js
const {
add, mul } = require("./a");
const sum = add(10, 20);
const mulRes = mul(100, 200);
console.log(sum);
console.log(mulRes);
commonjs 引用 npm 包
先初始化一下 npm 环境后下载 npm 包
yarn init -y
yarn add lodash
然后根据 npm 名称引用即可
const _ = require("lodash");
const arr = _.concat([1, 2], 3);
console.log(arr); // [1, 2, 3]
4. debugger
debugger 大家都很熟悉,在打断点的地方代码暂停,方便查找错误。
vscode 在 debug 的时候,会查找 package.json 的 main 属性指定的文件,这文件是主入口,默认名称为 index.js。
打断点后就可以点击左侧的调试入口便可以开始调试。

5. server 开发和前端开发的区别
并不是学会了 nodejs 就代表了会后端开发!
- 服务稳定性
- server 端可能会遭受各种恶意攻击和误操作
- 单个客户端可以意外挂掉,但是服务器不能
- 服务稳定性应用:PM2 进程守护
- 考虑内存和 CPU (优化,扩展)
- 客户端独占一个浏览器,内存和 CPU 都不是问题
- server 端要承载很多强求,CPU 和内存都是稀缺资源
- 使用 stream 写日志,使用 redis 存 session,优化内存
- 日志记录
- 前端也会参与写日志,但只是日志的发起方,不关心后续
- server 端要记录日志、存储日志、分析日志,前端不关心
- 安全
- server 端要随时准备接收各种恶意攻击,前端则少很多,如:越权操作,数据库攻击等
- 应用:登录验证,预防 xss 攻击和 sql 注入
- 集群和服务拆分
- 产品发展速度快,流量可能会迅速增加
- 通过扩展机器和服务拆分来承载更大流量
边栏推荐
- 系统集成项目管理工程师认证高频考点:编制项目范围管理计划
- 拓维信息使用 Rainbond 的云原生落地实践
- Memory Limit Exceeded
- Compilation problems and solutions of teamtalk winclient
- Pytorch learning (III)
- 金融服务行业SaaS项目管理系统解决方案,助力企业挖掘更广阔的增长服务空间
- 浏览器窗口切换激活事件 visibilitychange
- 深度学习编译器的理解
- Lenovo Yoga 27 2022, full upgrade of super configuration
- Teach you to quickly set up a live studio in 30 minutes
猜你喜欢

Full recharge, im+rtc+x full communication service "feedback season" starts

System integration project management engineer certification high frequency examination site: prepare project scope management plan

20220607跌破建议零售价,GPU市场正全面走向供过于求...

Swin-transformer --relative positional Bias

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

Merged binary tree of leetcode

一点比较有意思的模块

The easynvr platform equipment channels are all online. What is the reason for the "network request failure" in the operation?

PHP uses queues to solve maze problems

DTD modeling
随机推荐
ONEFLOW source code parsing: automatic inference of operator signature
Sword finger offer 16 Integer power of numeric value
OneFlow源码解析:算子签名的自动推断
PyTorch学习(三)
新版EasyGBS如何配置WebRTC视频流格式播放?
Cobbler轻松上手
The online procurement system of the electronic components industry accurately matches the procurement demand and leverages the digital development of the electronic industry
CODING 正式入驻腾讯会议应用市场!
Four tips tell you how to use SMS to promote business sales?
NBI可视化平台快速入门教程(五)编辑器功能操作介绍
NEON优化2:ARM优化高频指令总结
Electronic components bidding and purchasing Mall: optimize traditional purchasing business and speed up enterprise digital upgrading
Countdowncatch and completabilefuture and cyclicbarrier
EasyNVR平台设备通道均在线,操作出现“网络请求失败”是什么原因?
4个技巧告诉你,如何使用SMS促进业务销售?
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
Multipass Chinese document - setting graphical interface
The cloud native landing practice of using rainbow for Tuowei information
金融服务行业SaaS项目管理系统解决方案,助力企业挖掘更广阔的增长服务空间
Redis入门到精通01