前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Termux搭建nodejs环境

Termux搭建nodejs环境

作者头像
kongxx
发布2023-12-18 11:30:39
3810
发布2023-12-18 11:30:39
举报

安装nodejs

代码语言:javascript
复制
~ $ pkg install nodejs

使用http-server搭建文件下载服务

先安 http-server 并启动

代码语言:javascript
复制
# 安装 http-server 包
~ $ npm install -g http-server

# 启动 http-server 服务
~ $ http-server 

Starting up http-server, serving ./

http-server version: 14.1.1

http-server settings: 
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: false
Serve Brotli Files: false
Default File Extension: none

Available on:
  http://192.168.31.250:8080
  http://127.0.0.1:8080
Hit CTRL-C to stop the server

使用浏览器访问 http://192.168.31.250:8080/ 验证一下。

使用 express 框架搭建 web 服务

首先创建工程目录

代码语言:javascript
复制
~ $ mkdir myapp
~/myapp $ cd myapp

初始化nodejs工程

代码语言:javascript
复制
~/myapp $ npm init

Press ^C at any time to quit.
package name: (myapp) 
version: (1.0.0) 
description: 
entry point: (app.js)         <- 这里把默认的index.js改成app.js
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /data/data/com.termux/files/home/myapp/package.json:

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Is this OK? (yes) 

安装 express 框架

代码语言:javascript
复制
~/myapp $ npm install express

写个Hello World验证程序

代码语言:javascript
复制
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

启动服务

代码语言:javascript
复制
~/myapp $ node app.js
Example app listening on port 3000

使用浏览器访问 http://192.168.31.250:3000/ 验证。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-12-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装nodejs
  • 使用http-server搭建文件下载服务
  • 使用 express 框架搭建 web 服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档