当前位置:网站首页>快速掌握Nodejs安装以及入门
快速掌握Nodejs安装以及入门
2022-07-29 01:49:00 【Mighty CRUD】
一、Nodejs
1.1、Nodejs介绍
官网:http://nodejs.cn/
Node 是一个让 JavaScript 运行在服务端的开发平台,它让 JavaScript 成为与PHP、Python、Perl、Ruby 等服务端语言平起平坐的脚本语言。 发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装。
简单的说 Node.js 就是运行在服务端的 JavaScript。 Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台。底层架构是:javascript. 文件后缀:.js
Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好。

下载对应你系统的Node.js版本:
下载地址:https://nodejs.org/zh-cn/download
帮助文档:https://nodejs.org/zh-cn/docs
关于Nodejs:https://nodejs.org/zh-cn/about
1.2、安装
下载完成后打开 无脑安装下一步 默认安装即可
安装完成之后查看是否安装成功:
node -v

1.3、小结
Nodejs是一门计算机语言,运行在系统中的v8(jvm)引擎中。文件后缀是 js 运行的命令是:node
二、Nodejs快速入门
2.1、快速入门-Hello World
1、创建文件夹 nodejs
2、创建 helloworld.js
类似于java中的System.out.println("")
console.log('Hello World!!!')
运行:node helloworld.js
结果:hello world!!!
3、打开命令行终端:Ctrl + Shift + y
浏览器的内核包括两部分核心:
DOM渲染引擎;
- java script 解析器(js引擎)
- js运行在浏览器内核中的js引擎内部
4、小结
Node.js是脱离浏览器环境运行的JavaScript程序,基于V8 引擎
2.2、Node - 实现请求响应
1、创建 httpserver.js ;
// 导入模块是require 就类似于import java.io
const http = require('http');
// 1: 创建一个httpserver服务
http.createServer(function(request,response){
// 浏览器怎么认识hello server!!!
response.writeHead(200,{
'Content-type':'text/plain'}); //这句话的含义是:告诉浏览器将
// 以text-plain去解析hello server 这段数据。
// 给浏览器输出内容
response.end("<strong>hello server!!!</strong>");
}).listen(8888);
console.log("你启动的服务是:http://localhpst:8888以启动成功!!");
// 2: 监听一端口8888
// 3: 启动运行服务 node httpserver.js
// 4: 在浏览器访问http://localhost:8888
2、运行服务器程序;
node httpserver.js
3、服务器启动成功后,在浏览器中输入:http://localhost:8888/ 查看webserver成功运行,并输出html页面
4、停止服务:ctrl + c
2.3、Node-操作MYSQL数据库
参考:https://www.npmjs.com/package/mysql
1:安装mysql依赖
npm install mysql
2:定义db.js进行操作
//1: 导入mysql依赖包, mysql属于第三方的模块就类似于 java.sql一样的道理
var mysql = require("mysql");
// 1: 创建一个mysql的Connection对象
// 2: 配置数据连接的信息
var connection =mysql.createConnection({
host:"127.0.0.1",
port:3306,
user:"root",
password:"mkxiaoer",
database:"testdb"
});
// 3:开辟连接
connection.connect();
// 4: 执行curd
connection.query("select * from user",function(error,results,fields){
// 如果查询出错,直接抛出
if(error)throw error;
// 查询成功
console.log("results = ",results);
});
// 5: 关闭连接
connection.end();
// 最后一步:运行node db.js 查看效果
3:新建数据库:db_test和表kss_user表
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 60011
Source Host : localhost:3306
Source Schema : testdb
Target Server Type : MySQL
Target Server Version : 60011
File Encoding : 65001
Date: 20/01/2021 21:38:55
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of kss_user
-- ----------------------------
INSERT INTO `user` VALUES (1, '张三');
INSERT INTO `user` VALUES (2, '李四');
SET FOREIGN_KEY_CHECKS = 1;
4、运行db.js
node db
如果想开发更复杂的基于Node.js的应用程序后台,需要进一步学习Node.js的Web开发相关框架 express,art-template、koa等
边栏推荐
- Excel 用countif 统计
- The financing demand of 129 million yuan was released, and the roadshow of the Dake city project continued to irrigate the "good seedlings" of scientific innovation
- 如何在多御安全浏览器中自定义新标签页?
- The first of the five tips for using browsers efficiently is the most practical
- 如何把thinkphp5的项目迁移到阿里云函数计算来应对流量洪峰?
- 裂开了,一次连接池参数导致的雪崩问题
- PS + PL heterogeneous multicore case development manual for Ti C6000 tms320c6678 DSP + zynq-7045 (2)
- Resnet50 + k-fold cross validation + data enhancement + drawing (accuracy, recall, F value)
- How to customize a new tab in Duoyu security browser?
- 聊聊 Feign 的实现原理
猜你喜欢

响应式织梦模板装修设计类网站

Problems encountered in special flow & properties property set instances and Solutions

NPM install reports an error: eperm: operation not permitted, rename

C language improvement (I)

应用系统中的报表开发成本值多少?

防止勒索软件攻击数据的十种方法

Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning

如果非要在多线程中使用 ArrayList 会发生什么?

Remember error scheduler once Asynceventqueue: dropping event from queue shared causes OOM
![[simple implementation and extension of one · data | array heap]](/img/47/6f2c0d5c85cf05a24ba7adbd61e014.png)
[simple implementation and extension of one · data | array heap]
随机推荐
外包公司“混”了2年,我只认真做了5件事,如今顺利拿到字节 Offer。
Responsive Zhimeng template decoration design website
NPM install reports an error: eperm: operation not permitted, rename
Click the button to slide to the specified position
Resnet50 + k-fold cross validation + data enhancement + drawing (accuracy, recall, F value)
Full solution of 3D model format | including more than 70 kinds of RVT, 3ds, DWG, FBX, IFC, osgb, obj, etc
基于对象的实时空间音频渲染丨Dev for Dev 专栏
Vector similarity evaluation method
基于C51控制蜂鸣器
The first of the five tips for using browsers efficiently is the most practical
H5 background music is played automatically by touch
【golang学习笔记2.2】 Map、结构体和接口
[RT learning note 1] RT thread peripheral routine - control LED light flashing
awvs无法启动问题
7/28 高斯消元解线性方程组+高斯消元解异或线性方程组 +求组合数ii
"Activity recommendation" rush rush! 2022 international open source Festival has new content
ES6事件绑定(v-on用法)
Experiment 2: Arduino's tricolor lamp experiment
QT learning notes -37.qregex and regular expressions
千万不要把Request传递到异步线程里面,有坑