Set up express server with nodejs on iisnode

124 Views Asked by At

I have built a NodeJS server following the MVC architecture, connected to a SQL Server database.

Right now the whole backend server is running on localhost, what I want is to get the server online with iisnode on a windows server, I already installed everything that is needed iisnode, URL rerwrite and Node.

I'm a bit lost on the iisnode configuration level you saw that the last documentation was 12 years ago.

In my backend there are 3 files that I think can affect the execution of the backend in iisnode, they are the following, config/db.js, .env and server.js, below is the code.

config/db.js

// config \ db.js
import { Sequelize } from "sequelize";

const dbInstance = new Sequelize ('DB', 'LOGIN', 'PASSWORD', {
    dialect: 'mssql',
    host: '-',
    dialectOptions: {
        // Observe the need for this nested options field for MSSQL
        options: {
            useUTC: false,
            dateFirst: 1,
            enableArithAbort: true,
            encrypt: false,
            trustServerCertificate: true,
            instanceName: '-',
            trustedConnection: true,
            schema: 'ERPMASTER',
        }
    }
});

export { dbInstance };

.env

SERVER_HOST = -
SERVER_PORT = 4243 
TOKEN_SECRET = -

server.js

import express from "express";
import cors from "cors";
import morgan from "morgan";
import "dotenv/config.js";
import { routes } from "./routes.js";
import { dbInstance } from "./config/db.js";
import ErrorHandler from "./middlewares/ErrorHandler.js";

// Create express app
// -- REST SERVER -- //
const app = express();

// Cliente can be Postman | React | React Localhost link | etc
const clientURL = "*";

// Error Handler
app.use(ErrorHandler);

// CORS options
const corsOptions = {
  origin: clientURL,
};
app.use(cors(corsOptions));

// Output Logs
app.use(morgan("short"));

// Middleware

// Parse requests of content-type - application/json
app.use(express.json())

app.use(express.urlencoded({ extended: true }));

// -- ROUTES -- //
app.use("/api", routes);

async function startServer() {
  try {
    await dbInstance.authenticate();
    // Select all users from the database and log them to the console.
    console.log('Connection has been established successfully.');
    //await dbInstance.sync({ force: false, alter: true });
    app.listen(process.env.SERVER_PORT, process.env.SERVER_HOST, () => {
      console.log(
        "Server up and running at http://%s:%s",
        process.env.SERVER_HOST,
        process.env.SERVER_PORT
      );
    });
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
}

startServer();

I hope the api can be accessed by anyone, since the frontend built in React is already published

0

There are 0 best solutions below