14 Star 80 Fork 23

北京大学-张齐勋 / 移动端开发入门实践

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
NodeJS_环境配置.md 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
张齐勋 提交于 2021-07-07 04:48 . update

使用Node.js搭建一个后端Web服务

如何下载Node.js

https://nodejs.org/en/
下载后,上传到CentOS服务器上(可通过ftp,scp等方式上传,位置随意)

如何在CentOS上安装

https://github.com/nodejs/help/wiki/Installation

执行以下命令安装,将相应的版本号等信息修改为与自己下载的相符的即可

VERSION=v10.15.0
DISTRO=linux-x64
sudo mkdir -p /usr/local/lib/nodejs
sudo tar -xJvf node-$VERSION-$DISTRO.tar.xz -C /usr/local/lib/nodejs

修改 /etc/profile文件(vim /etc/profile),在最后增加以下内容

# Nodejs
VERSION=v10.15.0
DISTRO=linux-x64
export PATH=/usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin:$PATH

使配置文件生效

source /etc/profile

查看nodejs是否安装成功

node -v
npm version
npx -v

如何开发一个Hello World应用

https://nodejs.org/en/docs/guides/getting-started-guide/

编辑代码(在CentOS的任意位置均可),文件名可以为 app.js

const http = require('http');

const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader("Content-Type", "text/html; charset=utf-8")
  res.end('Hello World');
});

server.listen(port, () => {
  console.log(`Server running at http://ip:${port}/`);
});

运行应用

node app.js

通过浏览器可以访问该应用

设置npm镜像

npm config set registry https://registry.npm.taobao.org

恢复原来npm镜像

npm config set registry https://registry.npmjs.org/
JavaScript
1
https://gitee.com/ss-pku/webdev.git
git@gitee.com:ss-pku/webdev.git
ss-pku
webdev
移动端开发入门实践
master

搜索帮助