Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run html file using node js

I have a simple html page with angular js as follows:

    //Application name     var app = angular.module("myTmoApppdl", []);      app.controller("myCtrl", function ($scope) {         //Sample login function         $scope.signin = function () {             var formData =                     {                         email: $scope.email,                         password: $scope.password                     };         console.log("Form data is:" + JSON.stringify(formData));     }; }); 

HTML file:

<html>     <head>         <link href="bootstrap.min.css" rel="stylesheet" type="text/css"/>     </head>      <body ng-app="myTmoApppdl" ng-controller="myCtrl">         <div class="container">             <div class="form-group">                 <form class="form" role="form" method="post" ng-submit="signin()">                     <div class="form-group col-md-6">                         <label class="">Email address</label>                         <input type="email" class="form-control" ng-model="email" id="exampleInputEmail2" placeholder="Email address" required>                     </div>                     <div class="form-group col-md-6">                         <label class="">Password</label>                         <input type="password" class="form-control" id="exampleInputPassword2" ng-model="password" placeholder="Password" required>                     </div>                 </form>                 <button type="submit" class="btn btn-primary btn-block">Sign in</button>             </div>         </div>     </body>      <script src="angular.min.js" type="text/javascript"></script>      <!--User defined JS files-->     <script src="app.js" type="text/javascript"></script>     <script src="jsonParsingService.js" type="text/javascript"></script> </html> 

I am new to node js. I have installed node js server in my system but I am not sure how to run a simple html file using node js?

like image 599
kittu Avatar asked Mar 14 '16 18:03

kittu


People also ask

Can we connect HTML with node js?

Conclusion: With simple File IO operations we can read HTML file in Node. js and by using simple modules, we can send a HTML response back to client.


2 Answers

You can use built-in nodejs web server.

Add file server.js for example and put following code:

var http = require('http'); var fs = require('fs');  const PORT=8080;   fs.readFile('./index.html', function (err, html) {      if (err) throw err;          http.createServer(function(request, response) {           response.writeHeader(200, {"Content-Type": "text/html"});           response.write(html);           response.end();       }).listen(PORT); }); 

And after start server from console with command node server.js. Your index.html page will be available on URL http://localhost:8080

like image 125
Alexandr Avatar answered Sep 29 '22 19:09

Alexandr


Just install http-server globally

npm install -g http-server

where ever you need to run a html file run the command http-server For ex: your html file is in /home/project/index.html you can do /home/project/$ http-server

That will give you a link to accessyour webpages: http-server Starting up http-server, serving ./ Available on: http://127.0.0.1:8080 http://192.168.0.106:8080

like image 24
Vijaya Simha Avatar answered Sep 29 '22 18:09

Vijaya Simha