Build your static content server to serve files over HTTP using Node.js/Java

When I am writing code & testing it for my last article I came across below problem.

URL scheme must be "http" or "https" for CORS request

This error came because of the below code.

model = await tf.loadModel('model.json');

In above code, I am trying to load model.json from local file system which won’t work in that way. So I realised I need a static content server.

Sometimes we need a static content server to do our little experiments. We can do it easily with the help of Apache HTTP Server or Nginx. I feel we
already install a lot on our machines. I don’t like too many things installed on my machine so I don’t want to install Apache HTTP Server or Nginx just for the sake of serving static content.

Let’s create a simple HTML where we populate a table with the JSON data present in jsonData.json for example purpose for this article. Finally, we get output like this.

HTML code we are going to use to get above output

JSON data we are going to use to populate table

Let’s start building our own static content server.


Using Node.js

I am assuming you have Node.js & npm on your machine. We are going to use express framework for creating a static content server in Node.js. Navigate to the desired folder where you want to create your static content server in the terminal and run below commands.

/*You can use npm init too in the place of npm init -y for creating a project. To avoid asking answering the questions it prompts used npm init -y.*/
npm init -y
//Install express framework package
npm install express

Create an index.js file and paste below code.

Create a folder with name public. Paste table.html & jsonData.json in public folder which I mentioned above.

Run the following command to start your static content server.

node index.js

Hit http://localhost:3000/table.html URL in your browser.

Congratulations!!! your static content server up & running.

Whatever files you want to access through HTTP just paste in public folder that’s it. You will find the code for this at my GitHub Repository.


Using Java(Spring Boot)

I am assuming Java 8 & maven on your machine. We are going to use Spring Boot for creating a static content server in Java. I prefer Spring Boot because it reduces lots of development time and increases productivity. Go to http://start.spring.io/

Give proper Group & Artifact details and type web and select it in the text box below Search for dependencies. I chose 1.5.13 Spring Boot version because that is the latest one at the time of writing this blog for Java 8. Then click on Generate Project button.

Now a project is created with proper dependencies which we mentioned(web). Now, extract & open your project. Then paste table.html & jsonData.json in src/main/resources/static folder which I mentioned above.

Run the following command to start your static content server.

mvn spring-boot:run

Hit http://localhost:8080/table.html URL in your browser.

Congratulations!!! your static content server up & running.

Whatever files you want to access through HTTP just paste in src/main/resources/static folder that’s it. You will
find the code for this at my GitHub Repository.


Peace. Happy Coding.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *