Skip to content

Commit

Permalink
fix: migrate restify to express for non-sso-tab (#12640)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yimin-Jin authored Nov 1, 2024
1 parent ec065b0 commit 786f6c4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 55 deletions.
4 changes: 2 additions & 2 deletions templates/js/non-sso-tab/package.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "{{SafeProjectNameLowerCase}}",
"version": "0.1.0",
"engines": {
"node": "16 || 18"
"node": "18 || 20"
},
"private": true,
"dependencies": {
"restify": "^11.1.0",
"express": "^4.21.1",
"send": "^0.18.0"
},
"devDependencies": {
Expand Down
52 changes: 27 additions & 25 deletions templates/js/non-sso-tab/src/app.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
const restify = require("restify");
const send = require("send");
const express = require("express");
const fs = require("fs");
const https = require("https");
const path = require("path");
const send = require("send");

//Create HTTP server.
const server = restify.createServer({
key: process.env.SSL_KEY_FILE ? fs.readFileSync(process.env.SSL_KEY_FILE) : undefined,
certificate: process.env.SSL_CRT_FILE ? fs.readFileSync(process.env.SSL_CRT_FILE) : undefined,
formatters: {
"text/html": function (req, res, body) {
return body;
},
},
});
const app = express();

server.get(
"/static/*",
restify.plugins.serveStatic({
directory: __dirname,
})
);
const sslOptions = {
key: process.env.SSL_KEY_FILE ? fs.readFileSync(process.env.SSL_KEY_FILE) : undefined,
cert: process.env.SSL_CRT_FILE ? fs.readFileSync(process.env.SSL_CRT_FILE) : undefined,
};

server.listen(process.env.port || process.env.PORT || 3333, function () {
console.log(`\n${server.name} listening to ${server.url}`);
});
app.use("/static", express.static(path.join(__dirname, "static")));

// Adding tabs to our app. This will setup routes to various views
// Setup home page
server.get("/", (req, res, next) => {
send(req, __dirname + "/views/hello.html").pipe(res);
app.get("/", (req, res) => {
send(req, path.join(__dirname, "views", "hello.html")).pipe(res);
});

// Setup the static tab
server.get("/tab", (req, res, next) => {
send(req, __dirname + "/views/hello.html").pipe(res);
app.get("/tab", (req, res) => {
send(req, path.join(__dirname, "views", "hello.html")).pipe(res);
});

// Create HTTP server
const port = process.env.port || process.env.PORT || 3333;

if (sslOptions.key && sslOptions.cert) {
https.createServer(sslOptions, app).listen(port, () => {
console.log(`Express server listening on port ${port}`);
});
} else {
app.listen(port, () => {
console.log(`Express server listening on port ${port}`);
});
}
8 changes: 4 additions & 4 deletions templates/ts/non-sso-tab/package.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
"name": "{{SafeProjectNameLowerCase}}",
"version": "0.1.0",
"engines": {
"node": "16 || 18"
"node": "18 || 20"
},
"private": true,
"main": "./lib/app.js",
"dependencies": {
"restify": "^11.1.0",
"express": "^4.21.1",
"send": "^0.18.0"
},
"devDependencies": {
"@types/node": "^18.0.0",
"@types/restify": "^8.5.6",
"@types/express": "^5.0.0",
"@types/send": "^0.17.1",
"env-cmd": "^10.1.0",
"nodemon": "^2.0.21",
"nodemon": "^3.1.7",
"ts-node": "^10.9.1",
"typescript": "^4.1.2",
"shx": "^0.3.3"
Expand Down
50 changes: 26 additions & 24 deletions templates/ts/non-sso-tab/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import * as restify from "restify";
import express from "express";
import * as fs from "fs";
import * as https from "https";
import * as path from "path";
import send from "send";

//Create HTTP server.
const server = restify.createServer({
key: process.env.SSL_KEY_FILE ? fs.readFileSync(process.env.SSL_KEY_FILE) : undefined,
certificate: process.env.SSL_CRT_FILE ? fs.readFileSync(process.env.SSL_CRT_FILE) : undefined,
formatters: {
"text/html": (req, res, body) => {
return body;
},
},
});
const app = express();

server.get(
"/static/*",
restify.plugins.serveStatic({
directory: __dirname,
})
);
const sslOptions = {
key: process.env.SSL_KEY_FILE ? fs.readFileSync(process.env.SSL_KEY_FILE) : undefined,
cert: process.env.SSL_CRT_FILE ? fs.readFileSync(process.env.SSL_CRT_FILE) : undefined,
};

server.listen(process.env.port || process.env.PORT || 3333, function () {
console.log(`\n${server.name} listening to ${server.url}`);
});
app.use("/static", express.static(path.join(__dirname, "static")));

// Adding tabs to our app. This will setup routes to various views
// Setup home page
server.get("/", (req, res, next) => {
send(req, __dirname + "/views/hello.html").pipe(res);
app.get("/", (req, res) => {
send(req, path.join(__dirname, "views", "hello.html")).pipe(res);
});

// Setup the static tab
server.get("/tab", (req, res, next) => {
send(req, __dirname + "/views/hello.html").pipe(res);
app.get("/tab", (req, res) => {
send(req, path.join(__dirname, "views", "hello.html")).pipe(res);
});

// Create HTTP server
const port = process.env.port || process.env.PORT || 3333;

if (sslOptions.key && sslOptions.cert) {
https.createServer(sslOptions, app).listen(port, () => {
console.log(`Express server listening on port ${port}`);
});
} else {
app.listen(port, () => {
console.log(`Express server listening on port ${port}`);
});
}

0 comments on commit 786f6c4

Please sign in to comment.