使用Node.js搭建服务器

使用Node.js搭建服务器

1.安装Node.js和npm

  • 安装教程自行搜索(好多),建议Node.js直接安装在C盘

  • 注意镜像的设置:npm install -g cnpm --registry=https://registry.npm.taobao.org

  • 注意版本检查:

    //以下是我使用的版本号
    node -v  v14.21.2
    npm -v  6.14.17
    

2.创建一个空的文件夹,打开该路径下的终端(快捷键cmd+Enter)

  • 输入 npm init 初始化项目 ,设置一下 package name(正常起个名),后续一路回车即可,直到文件夹中出现 package.json 文件。
  • 注意:如果觉得麻烦直接 npm init -y 创建,生成一个默认的 package.json 文件

3.安装Express框架输入: npm i express

4.创建 server.js 文件,在文件中写上以下代码:

//导入express
const express = require('express');
//创建web服务器
const app = express();

// 通过app.listen进行服务器的配置,并启动服务器
//接收两个配置参数,一个是对应的端口号,一个是启动成功的回调函数
app.listen(5000,()=>{
    console.log('5000端口服务器启动成功');
})

//结合React的github案例的server.js文件
const express = require("express")
const axios = require("axios")
const app = express()

app.get("/search/users", function (req, res) {
  const {q} = req.query
  axios({
    url: 'https://api.github.com/search/users',
    params: {q}
  }).then(response => {
    res.json(response.data)
  }).catch(error => {
    // 捕获请求失败的情况,并返回适当的错误信息给客户端
    if (error.response) {
      // 请求已经发出,但是服务器返回了非2xx的状态码
      res.status(error.response.status).json({ error: error.response.statusText });
    } else if (error.request) {
      // 请求发出了,但是没有收到响应
      res.status(500).json({ error: "No response from server" });
    } else {
      // 其他错误
      res.status(500).json({ error: error.message });
    }
  });
})
 
app.get("/search/users2", function (req, res) {
  res.json({
    items: [
      {
        login: "yyx990803",
        html_url: "https://github.com/yyx990803",
        avatar_url:
          "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
        id: 1,
      },
      {
        login: "red456",
        html_url: "https://github.com/ruanyf",
        avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
        id: 2,
      },
      {
        login: "yyx9908032",
        html_url: "https://github.com/yyx990803",
        avatar_url:
          "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
        id: 3,
      },
      {
        login: "Banner",
        html_url: "https://github.com/ruanyf",
        avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
        id: 4,
      },
      {
        login: "yyx9908033",
        html_url: "https://github.com/yyx990803",
        avatar_url:
          "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
        id: 5,
      },
      {
        login: "abc3344",
        html_url: "https://github.com/ruanyf",
        avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
        id: 6,
      },
      {
        login: "yyx9908034",
        html_url: "https://github.com/yyx990803",
        avatar_url:
          "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
        id: 7,
      },
      {
        login: "apple822",
        html_url: "https://github.com/ruanyf",
        avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
        id: 8,
      },
      {
        login: "yyx9908035",
        html_url: "https://github.com/yyx990803",
        avatar_url:
          "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
        id: 9,
      },
    ],
  });
});

app.listen(5000, "localhost", (err) => {
  if (!err){
  	console.log("服务器启动成功")
  	console.log("请求github真实数据请访问:http://localhost:5000/search/users")
  	console.log("请求本地模拟数据请访问:http://localhost:5000/search/users2")
  } 
  else console.log(err);

5.运行服务器输入:node server.js

  • 如果你觉得麻烦,可以更改 package.json 文件的配置

    json 复制代码
    "scripts": {
        "start": "node server.js"
    },
  • 更改完毕运行服务器输入:npm start

相关推荐
热爱嵌入式的小许1 小时前
Linux基础项目开发1:量产工具——显示系统
linux·运维·服务器·韦东山量产工具
韩楚风5 小时前
【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
linux·服务器·性能优化·架构·gnu
陈苏同学5 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
Pythonliu75 小时前
茴香豆 + Qwen-7B-Chat-Int8
linux·运维·服务器
你疯了抱抱我6 小时前
【RockyLinux 9.4】安装 NVIDIA 驱动,改变分辨率,避坑版本。(CentOS 系列也能用)
linux·运维·centos
我是哈哈hh6 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
郭二哈6 小时前
C++——模板进阶、继承
java·服务器·c++
挥剑决浮云 -6 小时前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
立秋67896 小时前
Python的defaultdict详解
服务器·windows·python
Lansonli7 小时前
云原生(四十一) | 阿里云ECS服务器介绍
服务器·阿里云·云原生