I have a config that starts the server. I am making my project on typesript, do I need to typing this file? Do configs need to be typing or can they be left as they are?
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
if (process.env.NODE_ENV === "development") {
console.log("in development.");
} else {
console.log("in production.");
}
/* App Config */
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "../dist")));
/* Server Initialization */
app.get("/", (req, res) => res.sendFile("index.html"));
var port = process.env.PORT || 3000;
app.listen(port, () =>
console.log(
`Server initialized on: http://localhost:${port} // ${new Date()}`
)
);
CodePudding user response:
It is not strictly necessary to add types to this file, as it is mostly using built-in node modules, which already have their own types. However, it can be beneficial to do so for code readability and to catch any potential errors early. You can use the @types/express and @types/body-parser npm packages to add type definitions for Express and body-parser, respectively.
Additionally, you can use the ts-node package to run the server file with TypeScript. You may also need to configure a tsconfig.json file for your project, so TypeScript knows how to transpile your code.