Teng's blog Teng's blog
首页
Java
H5前端
GitHub (opens new window)
首页
Java
H5前端
GitHub (opens new window)
  • 快速入门

    • 简介
    • 简单使用
      • hello world
      • 创建简单的服务端程序
  • Framework-Nodejs
  • 快速入门
Shetengteng
2021-11-30

简单使用

示例地址

  • github (opens new window)
  • gitee (opens new window)

# hello world

  • 在项目路径下创建01.helloworld.js文件,输入如下内容
console.log('hello world')
1
  • 在vscode中点击01.helloworld.js右键,选择在集成终端中打开,输入如下命令执行该js文件
node .\01.helloworld.js
1

# 创建简单的服务端程序

  • 创建一个简单的服务端程序,每次访问,返回 hello world 在网页上
  • 新建02.simple-server.js
// 引入 http 模块,使用es5的语法
const http = require('http')
// 创建服务器
http.createServer(function (request, response) {
    // 发送HTTP头部
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200,{'Content-Type':'text/html'})
    // 发送响应的数据
    response.end('<h1>Hello World</h1>')
}).listen(9527)
// 启动完成后打印
console.log('Server running at http://127.0.0.1:9527/')
1
2
3
4
5
6
7
8
9
10
11
12
13
  • 启动服务,在控制台中输入如下命令
node .\02.simple-server.js
1
  • 关闭服务,在控制台中 ctrl+c
Last Updated: 2022/01/16, 11:29:51
简介

← 简介

Theme by Vdoing | Copyright © 2021-2022 Shetengteng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式