This tutorial will show you how to create a simple website in nodejs, deployable on Evernode.
1. Run npm init to initialize your project.
2. Install Express with npm i express.
3. Create a folder named public. This will hold your website files.
4. Inside public, create a file called index.html and add the text Hello World.
5. In the base directory (outside the public folder), create a file called index.js.
6. Open index.js and paste the server code. Note: the ports 36525, 36527, and 36529 are standard Evernode ports used for web listeners. The paths next to the optional env variables, KEY_PATH and CERT_PATH are where Evernode stores SSL certificates.
Server code:
const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
const PORTS = [36525, 36527, 36529]; // <- these are the standard evernode ports
const KEY_PATH = process.env.KEY_PATH || '/contract/cfg/tlskey.pem'; // <- this is the location for your instance ssl
const CERT_PATH = process.env.CERT_PATH || '/contract/cfg/tlscert.pem'; // <- this is the location for your instance ssl
let tlsOptions = null;
try {
if (fs.existsSync(KEY_PATH) && fs.existsSync(CERT_PATH)) {
tlsOptions = {
key: fs.readFileSync(KEY_PATH),
cert: fs.readFileSync(CERT_PATH),
};
console.log('TLS key and cert found — will start HTTPS servers.');
} else {
console.log('TLS key/cert not found — starting HTTP servers.');
}
} catch (err) {
console.error('Error reading TLS files, falling back to HTTP:', err && err.message);
tlsOptions = null;
}
const servers = PORTS.map(port => {
if (tlsOptions) {
const srv = https.createServer(tlsOptions, app).listen(port, '0.0.0.0', () => {
console.log(`HTTPS: https://localhost:${port}/`);
});
srv.on('error', e => console.error(`Port ${port} error:`, e.code || e.message));
return srv;
} else {
const srv = http.createServer(app).listen(port, '0.0.0.0', () => {
console.log(`HTTP: http://localhost:${port}/`);
});
srv.on('error', e => console.error(`Port ${port} error:`, e.code || e.message));
return srv;
}
});
7. In your terminal, make sure you’re in the same folder as index.js, then run node index.js. You’ve now started the webserver! Since it’s running locally, it will use http. On Evernode, it will use https.
8. Open your browser and go to http://localhost:36525 to view your website.
Pre-requisites:
Install NPM
Install Node.js