首页 > 解决方案 > How to set port for Heroku on a Node.Js Typescript Project

问题描述

Normally, Heroku dynamically sets the port for us.

const PORT : string|number = process.env.PORT || 5000;

But how do I modify this code to accept PORT... and it's using the => shorthand with Typescript.

server.listen(port => {
  console.log(`Server is listening on http://localhost:${port}`);
});

Here's the top of the server class...

import express, { Application } from "express";
import socketIO, { Server as SocketIOServer } from "socket.io";
import { createServer, Server as HTTPServer } from "http";
import path from "path";

export class Server {
  private httpServer: HTTPServer;
  private app: Application;
  private io: SocketIOServer;

标签: node.jstypescriptheroku

解决方案


您需要在 http.createserver 中指定应用程序模块,然后只有 express 可以通过端口携带应用程序

const PORT = process.env.PORT || 8888;

http.createServer(app).listen(PORT, () => {
    console.log(PORT);
})

推荐阅读