Skip to content

Commit

Permalink
wwwファイルのts化
Browse files Browse the repository at this point in the history
  • Loading branch information
katkatprog committed Jun 3, 2022
1 parent af5863c commit f806c04
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/bin/www.ts
Expand Up @@ -4,22 +4,23 @@
* Module dependencies.
*/

var app = require("../app");
var debug = require("debug")("sample:server");
var http = require("http");
import app from "../app";
import * as http from "http";
import * as debugModule from "debug";
var debug = debugModule.debug("quick-start-express-typescript:server");

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || "3000");
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);
const server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
Expand All @@ -33,17 +34,17 @@ server.on("listening", onListening);
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);
function normalizePort(val: string): number | string | boolean {
const nport = parseInt(val, 10);

if (isNaN(port)) {
if (isNaN(nport)) {
// named pipe
return val;
}

if (port >= 0) {
if (nport >= 0) {
// port number
return port;
return nport;
}

return false;
Expand All @@ -53,23 +54,21 @@ function normalizePort(val) {
* Event listener for HTTP server "error" event.
*/

function onError(error) {
function onError(error: any): void {
if (error.syscall !== "listen") {
throw error;
}

var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
const bind = typeof port === "string" ? "Pipe " + port : "Port " + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
Expand All @@ -79,8 +78,21 @@ function onError(error) {
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
function onListening(): void {
function bind() {
const addr = server.address();
if (addr === null) {
return "";
}

if (typeof addr === "string") {
return "pipe " + addr;
}

if ("port" in addr) {
return "port " + addr.port;
}
}

debug("Listening on " + bind());
}

0 comments on commit f806c04

Please sign in to comment.