当前位置:网站首页>快速掌握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等
边栏推荐
- ES6 syntax extension
- Excel 打开包含汉字的 csv 文件出现乱码该怎么办?
- Kubesphere multi node installation
- Esbuild Bundler HMR
- MySQL之数据查询(多表查询)
- Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning
- Responsive dream weaving template home decoration website
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3, sign in question cajhf
- Kubesphere-多节点安装
- Three methods of STM32 delay function
猜你喜欢

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

"Wei Lai Cup" 2022 Niuke summer multi school training camp 2, sign in question GJK

Responsive dream weaving template hotel room website

C语言提高篇(一)

Rgbd point cloud down sampling
![[cloud native] what is the microservice architecture](/img/84/a0ec68646083f3539aa39ad9d98749.png)
[cloud native] what is the microservice architecture

记一次 ERROR scheduler.AsyncEventQueue: Dropping event from queue shared导致OOM

会议OA之会议通知

Idea connection database

webview攻击
随机推荐
12. < tag dynamic programming and subsequence, subarray> lt.72. edit distance
C语言提高篇(一)
Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning
进程间通信---对管道的详细讲解(图文案例讲解)
响应式织梦模板酒店客房类网站
聊聊 Feign 的实现原理
Kubesphere-多节点安装
ES6 syntax extension
Prometheus + AlertManager 消息预警
autoware中ndtmatching功能加载点云图坐标系修正的问题
Keil5 open the engineering prompt not found device solution
第3章业务功能开发(线索备注的删除和修改)
Navigation -- realize data transmission and data sharing between fragments
mobile-picker.js
Website Collection
Idea connection database
WebView attack
[mqtt from introduction to improvement series | 09] Wireshark packet capturing and analyzing mqtt messages
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2, sign in question GJK
如何利用 RPA 实现自动化获客?