add node express & mysql starter template
This commit is contained in:
41
web/other/node-express-ts-mysql-starter/src/db.ts
Normal file
41
web/other/node-express-ts-mysql-starter/src/db.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import mysql from "mysql2/promise";
|
||||
|
||||
const user = process.env.MYSQL_USER;
|
||||
const password = process.env.MYSQL_PASSWORD;
|
||||
const database = process.env.MYSQL_DATABASE;
|
||||
const host = process.env.MYSQL_HOST;
|
||||
const port = process.env.MYSQL_PORT;
|
||||
|
||||
if (!user) {
|
||||
throw new Error("MYSQL_USER is not set");
|
||||
}
|
||||
|
||||
if (!password) {
|
||||
throw new Error("MYSQL_PASSWORD is not set");
|
||||
}
|
||||
|
||||
if (!database) {
|
||||
throw new Error("MYSQL_DATABASE is not set");
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
throw new Error("MYSQL_HOST is not set");
|
||||
}
|
||||
|
||||
if (!port) {
|
||||
throw new Error("MYSQL_PORT is not set");
|
||||
}
|
||||
|
||||
if (isNaN(parseInt(port))) {
|
||||
throw new Error("MYSQL_PORT is not a number");
|
||||
}
|
||||
|
||||
export async function connect() {
|
||||
return await mysql.createConnection({
|
||||
host,
|
||||
user,
|
||||
password,
|
||||
database,
|
||||
port: parseInt(port || "3306"),
|
||||
});
|
||||
}
|
||||
15
web/other/node-express-ts-mysql-starter/src/index.ts
Normal file
15
web/other/node-express-ts-mysql-starter/src/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import express from "express";
|
||||
import { config } from "dotenv";
|
||||
|
||||
config();
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
app.get("/", (req: express.Request, res: express.Response) => {
|
||||
res.send("Express + TypeScript Server");
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`[server]: Server is running at http://localhost:${port}`);
|
||||
});
|
||||
Reference in New Issue
Block a user