Start very simple web server

Hi all,

I’m developping a simple html/css/js website using NodeJS http-server to render content. I’m noob in deployment stuff so I’m a bit lost

In order to start my server and access to its content I use the command “node server/server.js”, and when the web server is started my prompt displayes: “Server started on http://localhost:3000/”, and in my browser I can see my website on localhost:3000. So it’s already working well locally, I don’t have any issue

My problem: I don’t know how to deal with the deployment process, any idea ? How would you do ?

Thanks in advance

Hi @Nounoune, thanks for the post and welcome to the Netlify Support Forums.

First of all if you want to host a Node.js application on Netlify you have to use serverless function.
Kindly read the blog post below on how to get started with Netlify Functions.

Also I don’t know how your project has been structured but below is a sample of how you can serve an HTML once you understand the basics of the Netlify Functions.

const fs = require('fs');
const path = require('path');

exports.handler = async (event, context) => {
  const { path: requestedPath } = event.rawPath;

  try {
    // Replace 'public' with the path to your static files directory
   // containing the index.html file. The requestedPath should match the 
   // filePath on the server
    const filePath = path.join(__dirname, 'public', requestedPath);

    // Read the file content
    const fileContent = fs.readFileSync(filePath, 'utf-8');

    return {
      statusCode: 200,
      body: fileContent,
      headers: {
        'Content-Type': 'text/html',
      },
    };
  } catch (error) {
    return {
      statusCode: 404,
      body: 'File not found',
    };
  }
};

Hope the above helps.

Hi @clarnx, thanks for replying

My NodeJS server is configured like that:

const express = require("express");
const fs = require("fs");
const app = express();

app.use(express.static("public"));

function includeHeaderAndFooter(content) {
    const header = fs.readFileSync("public/html/partials/header.html", "utf8");
    const footer = fs.readFileSync("public/html/partials/footer.html", "utf8");
    return `${header}${content}${footer}`;
}

app.get("/", (req, res) => {
    let content = fs.readFileSync("public/html/pages/news.html", "utf8");
    const pageContent = includeHeaderAndFooter(content);
    res.send(pageContent);
});

app.get("/about", (req, res) => {
    const content = fs.readFileSync("public/html/pages/about.html", "utf8");
    const pageContent = includeHeaderAndFooter(content);
    res.send(pageContent);
});

app.use((req, res) => {
    res.status(404).send(`You don't have the right to access to this file`);
});

const PORT = process.env.PORT || 3000;
// app.listen(PORT);

const server = app.listen(PORT, () => {
    console.log(`Server started on http://localhost:${PORT}/`);
});

process.on("SIGTERM", () => {
    console.log("Stopping server...");
    server.close(() => {
        console.log("Server stopped.");
        process.exit(0);
    });
});

Its aim is only to build and serve html pages, depending on what you type in the url, and I don’t know how to start it from Netlify
Maybe I didn’t use the appropriate strcture to do what I want ?
Do I have to replace all this stuff by serverless functions ?

Hi, @Nounoune. Netlify doesn’t allow access to the Node.js runtime after deployment, with the exception of serverless functions.

You can try modifying your code to run as a serverless function as @clarnx has suggested. However, there are other options as well.

For example, there are also other hosting providers that will allow access to a Node.js runtime environment for a deployed site. This will allow you to use your existing code without modifying it. Using a popular example, hosting your site at Heroku could be done without changes (or at least without any significant changes).

We would love to have you host your site here at Netlify but it seems that your site’s design is a better fit for other hosting. I just don’t want you doing a bunch of work to “do things the Netlify way” when other options exist with less effort. However, if there are questions about making the code serverless function ready, please let us know.

2 Likes

Hi @luke, thanks for your response, I think I will try another hosting provider
Thanks guys !