From 786f6c493b69ef3d44592a0bef3a1536f19cf5af Mon Sep 17 00:00:00 2001 From: Yimin-Jin <139844715+Yimin-Jin@users.noreply.github.com> Date: Fri, 1 Nov 2024 15:21:49 +0800 Subject: [PATCH] fix: migrate restify to express for non-sso-tab (#12640) --- templates/js/non-sso-tab/package.json.tpl | 4 +- templates/js/non-sso-tab/src/app.js | 52 ++++++++++++----------- templates/ts/non-sso-tab/package.json.tpl | 8 ++-- templates/ts/non-sso-tab/src/app.ts | 50 +++++++++++----------- 4 files changed, 59 insertions(+), 55 deletions(-) diff --git a/templates/js/non-sso-tab/package.json.tpl b/templates/js/non-sso-tab/package.json.tpl index e2c7c176a3..f1237ada49 100644 --- a/templates/js/non-sso-tab/package.json.tpl +++ b/templates/js/non-sso-tab/package.json.tpl @@ -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": { diff --git a/templates/js/non-sso-tab/src/app.js b/templates/js/non-sso-tab/src/app.js index 8eb03c099c..478d3e72cb 100644 --- a/templates/js/non-sso-tab/src/app.js +++ b/templates/js/non-sso-tab/src/app.js @@ -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}`); + }); +} diff --git a/templates/ts/non-sso-tab/package.json.tpl b/templates/ts/non-sso-tab/package.json.tpl index fa8b15cff4..892cf6d149 100644 --- a/templates/ts/non-sso-tab/package.json.tpl +++ b/templates/ts/non-sso-tab/package.json.tpl @@ -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" diff --git a/templates/ts/non-sso-tab/src/app.ts b/templates/ts/non-sso-tab/src/app.ts index 168059bc8c..00174d8ef7 100644 --- a/templates/ts/non-sso-tab/src/app.ts +++ b/templates/ts/non-sso-tab/src/app.ts @@ -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}`); + }); +}