Skip to content

Instantly share code, notes, and snippets.

@deomorxsy
Last active September 28, 2021 21:47
Show Gist options
  • Save deomorxsy/57c0bc26299d08024d7f43f2993119d8 to your computer and use it in GitHub Desktop.
Save deomorxsy/57c0bc26299d08024d7f43f2993119d8 to your computer and use it in GitHub Desktop.
express server with nodejs to serve a web page inside a Termux environment.

Servindo servidor web com páginas estáticas no ambiente Termux

tags: nodejs, express

Temos algumas alternativas para rodar páginas web escritas dentro do ambiente Termux. Uma delas é utilizando o nodejs, que é o escopo desse tutorial.

  1. Instale nodejs caso ainda não tenha feito isso
; pkg install nodejs
  1. Crie um diretório e entre nele:
; mkdir -p ~/codigos/web-with-nodejs/
; cd ~/codigos/web-with-nodejs/
  1. npm init: aceite todas as opções padrão.
; npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (web)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /home/asari/Documentos/coding/node-web/package.json:
{
  "name": "web",
  6 const express = require("express");
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
Is this OK? (yes)
npm notice
npm notice New minor version of npm available! 7.21.1 -> 7.24.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v7.24.0
npm notice Run npm install -g [email protected] to update!
  1. Instale o framework express do nodejs:
; npm install express
added 50 packages, and audited 51 packages in 3s
found 0 vulnerabilities
  1. Crie o arquivo index.js.
; vim index.js
  1. Cole o código abaixo dentro do arquivo index.js para iniciar um servidor que serve páginas web estáticas.
const express = require("express");

const app = express();
const port = 3000;

app.use(express.static("public")) ,

app.listen(port, () => {
  console.log(`server listening at ${port}`);
});
  1. Crie uma pasta chamada "public". Todos os arquivos html, css, e javascript que ligam diretamente ao site principal devem ser colocados lá.
mkdir public
  1. Inicie o servidor rodando:
node index.js
  1. No navegador, digite a seguinte URL:
http://localhost:3000/nome-do-teu-arquivo.html

E a página web deve aparecer. Perceba que o nome-do-teu-arquivo.html deve estar dentro da pasta public, senão não será encontrado.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment