Nodejs example app (#52)

* Fix node deploy path handling.

This patch uses a temporary symlink to envs/APP/node_modules to force
node to install the modules into the right place even on older node
versions, like v4.2.6 which ships with Unbuntu 16.04.

Apps launching node servers will need to manually set
NODE_PATH="${VIRTUAL_ENV}/node_modules" to have it run correctly, and
in future we should consider setting this in spawn_app (though currently
spawn_app does not know what kind of app it's spawning).

* Add NODE_PATH to spawn env if node env present.

* Added nodejs websocket chat example app.
pull/54/head
Chris McCormick 2019-06-28 19:58:36 +08:00 zatwierdzone przez Rui Carmo
rodzic e43b318d61
commit a4ded8c994
7 zmienionych plików z 188 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1 @@
AUTO_RESTART=1

Wyświetl plik

@ -0,0 +1,2 @@
web: ./index.js 2>&1 | cat
release: ./hello.js

Wyświetl plik

@ -0,0 +1,25 @@
# Nodejs Sample Application
This is a simple Nodejs app for piku that demonstrates:
* Simple multi-user websocket chat application.
* A nodejs web worker which handles incoming requests.
* A release worker which fires at deploy time (hello.js).
To publish this app to `piku`, make a copy of this folder and run the following commands:
```bash
git init .
git remote add piku piku@your_server:nodechat
git add .
git commit -a -m "initial commit"
git push piku master
```
Then you can set up an SSL cert and connect a domain by setting config variables like this:
```bash
ssh piku@your_server config:set nodechat NGINX_SERVER_NAME=your_server NGINX_HTTPS_ONLY=1
```
Then visit the site `your_server` and you will see a simple websocket chat application.

Wyświetl plik

@ -0,0 +1,5 @@
#!/usr/bin/env nodejs
console.log("Hello!");
console.log(process.argv)
console.log(require('express') ? "express exists" : "express missing");

Wyświetl plik

@ -0,0 +1,117 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>HTML5 Minimal template</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="minimal.css" >
<link rel="stylesheet" type="text/css" href="minimal-inputs.css" >
</head>
<body>
<p id="connecting">Connecting...</p>
<div id="interface">
<input type="text" id="nickname" class="quarter" placeholder="Nick name..."/>
<input type="text" id="message" class="half" placeholder="Message text..."/>
<button id="send" class="quarter">Send</button>
<pre id="output"></pre>
</div>
</body>
<script>
var el = function(id) { return document.getElementById(id) };
var ws = new WebSocket(document.location.href.replace("http", "ws") + "ws/");
ws.onopen = function() {
console.log("websocket opened", arguments);
el("connecting").style.display = "none";
el("interface").style.display = "block";
};
ws.onmessage = function(message) { el("output").textContent += message.data + "\n"; };
ws.onclose = function() { console.log("close"); };
var send = function(ev) {
ws.send(JSON.stringify({
"name": el("nickname").value,
"message": el("message").value,
}));
el("message").value = "";
el("message").focus();
}
el("send").onclick = send;
el("message").onkeyup = function(ev) {
if (ev.keyCode == 13) {
send(ev);
}
}
</script>
<style>
* {
box-sizing: border-box;
}
body {
color: #555;
margin: 0 auto;
max-width: 50em;
font-family: "Helvetica", "Arial", sans-serif;
line-height: 1.5;
padding: 2em 1em;
}
pre {
border-left: 2px solid #bbb;
padding: 1em;
}
a {
color: #f56a6a;
}
input[type="submit"], input[type="reset"], input[type="button"], button, .button {
box-shadow: inset 0 0 0 2px #f56a6a;
color: #f56a6a;
font-weight: 700;
letter-spacing: 0.075em;
text-transform: uppercase;
text-align: center;
font-size: 0.8em;
}
input[type="submit"], input[type="reset"], input[type="button"], button, .button, input[type="text"], input[type="password"], input[type="email"], input[type="tel"], select, textarea {
border-radius: 0.375em;
background-color: transparent;
border: 0;
cursor: pointer;
display: inline-block;
min-height: 3.5em;
line-height: 3.5em;
padding: 0 2.25em;
text-decoration: none;
margin: 0.25em 0.25em;
font-size: 0.8em;
}
input[type="text"], input[type="password"], input[type="email"], input[type="tel"], select, textarea {
background-color: #eee;
color: #555;
outline: none;
padding: 0 0.75em;
font-weight: bold;
}
.half {
width: calc(50% - 0.7em);
}
.quarter {
width: calc(25% - 0.75em);
}
#interface {
display: none;
}
</style>
</html>

Wyświetl plik

@ -0,0 +1,30 @@
#!/usr/bin/env nodejs
const express = require('express');
const expressws = require('express-ws');
const fs = require('fs');
const app = express();
const ews = expressws(app);
app.get('/', (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.ws('/ws', (ws, req) => {
ws.on('message', msg => {
console.log("Got message:", msg);
ews.getWss().clients.map(function(c) {
c.send(msg);
});
});
ws.on('close', () => {
console.log('WebSocket was closed');
});
})
var port = process.env["PORT"] || 8000;
var addr = process.env["BIND_ADDRESS"] || "127.0.0.1";
console.log("Starting app on " + addr + ":" + port);
app.listen(port, addr, () => console.log('App launched.'));

Wyświetl plik

@ -0,0 +1,8 @@
{
"dependencies": {
"express": "^4.17.1",
"express-ws": "^2.0.0"
},
"description": "Piku node example.",
"name": "piku-example"
}