CodeWithMayank

Deploying Flutter Web With Node.js Server | Step-by-Step | 100% Working

Introduction:

In this tutorial, we will learn how to deploy a Flutter web application using Node.js. Flutter provides a powerful framework for building cross-platform applications, and with the help of Node.js, we can host our Flutter web app on a server. Let’s get started with the deployment process:

Copying Flutter Web Files

To get started, we need to generate the web files for our Flutter application. Here’s how you can do it:

  1. Open your terminal or command prompt.
  2. Navigate to your Flutter project directory.
  3. Build the Flutter web application by running the following command

This command compiles your Flutter code and generates the necessary web files in the build/web directory.

Pasting Node.js Code

Next, we’ll create a Node.js project and configure it to serve our Flutter web files. Follow these steps:

  • Create a new directory for your Node.js project.
  • Open your favorite text editor and create a new file named app.js.
  • Copy and paste the following code into app.js:

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


app.use(express.static(path.join(__dirname, "public-flutter")));

app.get("*", (req, res) => {
    res.sendFile(path.join(__dirname, "public-flutter/index.html"));
});

const port = 3000; // Choose a suitable port number
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});

  • Save the file.
  • In the same project directory, create a package.json file by running the following command:
  • Open package.json and replace its content with the following:

{
    "name": "your_project_name",
    "version": "0.0.0",
    "private": true,
    "scripts": {
        "start": "node ./bin/www"
    },
    "dependencies": {
        "cookie-parser": "~1.4.4",
        "debug": "~2.6.9",
        "express": "^4.18.2",
        "morgan": "~1.9.1"
    }
}

Running the Flutter Web Application

We are now ready to run our Flutter web application using the Node.js server. Follow these steps:

  1. Open your terminal or command prompt.
  2. Navigate to the root directory of your Node.js project.
  3. Install the dependencies by running the following command:
  • Copy the Flutter web files into the Node.js project’s public-flutter folder
flutter web
  • After successfully copying the Flutter web files into the Node.js project’s public-flutter folder, you are good to go. Now, run the following command in the terminal or command prompt window of your Node.js project to start the server:
  • Congratulations! Your Flutter web application is now running on the Node.js server. Open your web browser and visit http://localhost:3000 to see your application in action.

Conclusion

In this blog post, we learned how to build a Flutter web application and host it using a Node.js server with Express. By combining the power of Flutter for the frontend and Node.js for the backend, we can create robust and performant web applications. We covered the steps of copying the Flutter web files, pasting the necessary code into a Node.js project, and running the application. Now you have the knowledge to create your own Flutter web applications and leverage the capabilities of Node.js for hosting and serving your application. Happy coding!

 

Scroll to Top