编写第一个node.js实例
2023-09-14 09:04:08 时间
前提:安装了nodejs Windows版本,傻瓜式下一步安装成功,在cmd输入node -v如果显示了版本号就说明安装成功了 在我们创建 Node.js 第一个 "Hello, World!" 应用前,让我们先了解下 Node.js 应用是由哪几部分组成的: 1,引入 required 模块:我们可以使用 require 指令来载入 Node.js 模块。
var http = require('http');
2,创建服务器:服务器可以监听客户端的请求,类似于 Apache 、Nginx 等 HTTP 服务器。
http.createServer(function (request, response) {
response.writeHead(200,{'Content-Type':'text/plain'});
response.end('Hello World\n');
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888');
3,接收请求与响应请求 服务器很容易创建,客户端可以使用浏览器或终端发送 HTTP 请求,服务器接收请求后返回响应数据。
在cmd里面 进入对应文件目录:node 文件名.js
运行成功后,打开浏览器:http://127.0.0.1:8888/
大功告成!!!
完整的server.js代码:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888');
简单的说 Node.js 就是运行在服务端的 JavaScript。
Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台。
Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好。
相关文章
- 初识js中的闭包
- JS 中的日期时间操作计算实例
- JS练习实例--编写经典小游戏俄罗斯方块
- js实现『加载更多』功能实例
- Node.js事件驱动模型
- JS魔法堂:再识Number type
- js笔记——call,apply,bind使用笔记
- JS传值OR传引用
- js中获取项目路径的小插件
- js: TransformStream 浏览器上的流转换
- [Node.js] Polling
- [Node.js] Apply image filter on Express
- [Node.js] Add Logging to a Node.js Application using Winston
- SAP UI5 应用开发教程之四十 - 如何制作包含了 component-preload.js 在内的 SAP UI5 发布版本
- Atitit nlp自然语言处理类库(java python nodejs c#net) 目录 1.1. Python snownlp1 1.2. NLP.js一个nodejs/javascri
- js全屏切换效果demo效果示例(整理)
- javascript案例18——js加减乘除
- js div禁止点击,恢复点击(jquery)
- Egg.js+Vant前后端实例
- Vue服务端渲染Nuxt.js实例
- React.js 集成 Kotlin Spring Boot 开发 Web 应用实例详解
- JS 正则判断输入数据是否合法实例(正则表达式表单验证)