Nodejs之搭建本地服务器

Nodejs之搭建简单服务器

如下内容转载编辑自菜鸟教程, 笨笨熊咦html

如下内容相应代码已上传至GitHub:https://github.com/tsora-c/node-servernode

  1. 引入required模块

咱们使用 require 指令来载入 http 模块,并将实例化的 HTTP 赋值给变量 http,实例以下:git

const http=require("http");
  1. 建立服务器

接下来咱们使用 http.createServer() 方法建立服务器,并使用 listen 方法绑定 8888 端口。 函数经过 request, response 参数来接收和响应数据。github

实例以下,在你项目的根目录下建立一个叫 server.js 的文件,并写入如下代码:web

const http =require("http");
// 设置端口
const post=8888;
http.createServer((request,response)=>{
    // 发送 HTTP 头部 
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200,{
        "Content-Type":"text/plain"
    });
    // 发送响应数据 "Hello World"
    response.write("Hello World\n"); 
    // 结束
    response.end();
}).listen(post);
// 终端打印以下信息
console.log("Server running at http://127.0.0.1:"+post+"/");

以上代码咱们完成了一个能够工做的 HTTP 服务器。浏览器

使用 node 命令执行以上的代码:服务器

node server.js
Server running at http://127.0.0.1:8888/

接下来,打开浏览器访问 http://127.0.0.1:8888/,你会看到一个写着 "Hello World"的网页。svg

到这里,一个简单的服务器就基本完成了,可是仅能显示这些并非服务器该作的事函数

  1. 服务器首页

一个服务器起码得有个显示首页,做为服务器的门户;post

新建一个简单的index.html做为服务器首页,代码以下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>Index</h1>
</body>
</html>

那么就须要读取html文件的内容,node中的File System模块就提供这样的功能

修改server.js代码

// 声明http协议
const http = require("http");
// 声明文件操做系统对象
const fs = require("fs");
// 声明端口
const post = 8888;
http.createServer((request, response) => {
    // 发送 HTTP 头部 
    // HTTP 状态值: 200 : OK
    // 内容类型: text/html
    response.writeHead(200, {
        "Content-Type": "text/html"
    });
    fs.readFile("./index.html", 'utf-8', (err, data) => {
        if (err) {
            throw err;
        }
        // 发送响应数据
        response.write(data);
        // 结束
        response.end();
    })

}).listen(post);
// 终端打印以下信息
console.log("Server running at http://127.0.0.1:" + post + "/");

打开浏览器访问 http://127.0.0.1:8888/,网页就已经变成index页面。

  1. 服务器多页面

一样,一个服务器仅有一个首页也是远远不够的;

新建一个简单的about.html做为服务器介绍页面,代码以下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>about</h1>
</body>
</html>

那么该如何切换页面?很显然,就是根据地址栏来判断加载相应的页面文件;

那么这些相应的信息在哪里?就在http.createServer的回调函数中request对象中

修改server.js代码

// 声明http协议
const http = require("http");
// 声明文件操做系统对象
const fs = require("fs");
// 声明端口
const post = 8888;
http.createServer((request, response) => {

    const url = request.url;
    // 发送 HTTP 头部 
    // HTTP 状态值: 200 : OK
    // 内容类型: text/html
    response.writeHead(200, {
        "Content-Type": "text/html"
    });
    if (url === "/" || url === "/index") {
        fs.readFile("./index.html", 'utf-8', (err, data) => {
            if (err) {
                throw err;
            }
            // 发送响应数据
            response.write(data);
            // 结束
            response.end();
        })
    }
    else{
        fs.readFile("./about.html", 'utf-8', (err, data) => {
            if (err) {
                throw err;
            }
            // 发送响应数据
            response.write(data);
            // 结束
            response.end();
        })
    }

}).listen(post);
// 终端打印以下信息
console.log("Server running at http://127.0.0.1:" + post + "/");

打开浏览器访问 http://127.0.0.1:8888/http://127.0.0.1:8888/indexhttp://127.0.0.1:8888/about,网页就已经变成index页面。

  1. 调整文件位置

为了方便代码维护以及读取,咱们就须要修改文件相应位置以及命名

固然这些依据我的习惯以及项目要求设定

如下仅表明我的观点

在这里插入图片描述

  1. 拓展

基于现学的相应知识,咱们能够拓展完成哪下内容

  • 简单服务器
  • 简单路由