0

I want to use my original html in node.js

This is simple hsh.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> How to Say Hello </title>
    <link type="text/css" href="./sys/lib/css/uniform.default.css" rel="stylesheet" media="screen" />
    <link type="text/css" href="./sys/lib/css/jquery-ui.1.10.3.smoothness.css" rel="stylesheet" media="screen" />
    <script type="text/javascript" src="./sys/lib/scripts/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="./sys/lib/scripts/jquery-ui.1.10.3.min.js"></script>
    <script type="text/javascript" src="./sys/lib/scripts/myhello.js"></script>
    <script>
    $(function(){
        $( "#sayDate" ).datepicker();
    });

    function resetHello()
    {
        document.getElementById("hello").value = "";
        document.getElementById("sayDate").value = "";
    }
    </script>
</head>
<body>
    <form name="syaHello">
        How to say hello in your contry?<br>
        <input type="text" id="hello" value="">
        <INPUT id=sayDate style="WIDTH: 100px" name=sayTime>
    </form>
    <div class="docBtn_list">
        <input type="button" value="View Hello" onclick="javascript:howHello();" /> 
        <input type="button" value="Reset" onclick="resetHello();" /> 
    </div>
</body>
</html>

myhello.js

function howHello()
{
    alert(document.getElementById("hello").value + " " + 
          document.getElementById("sayDate").value);
}

and nodeSev.js

var http = require('http'),
    fs = require('fs');


fs.readFile('./hsh.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(3000);
});

But this is not working about jquery and howHello java script.

I don't want change html and js too much and don't use express package.

4
  • 2
    One problem, is that you will write back the html for every request including those for your static assets (.js, .css, etc). You need to implement your own routing logic for that or use a framework. Commented Nov 11, 2013 at 4:43
  • 3
    The question is wrong. It has nothing to do with jQuery. It should read "How can I serve static files?". And those guys who really want to use jQuery server-side, have a look at jsdom. Commented Nov 11, 2013 at 8:02
  • 1
    The HTML5 doctype is simply <!doctype html> Commented Nov 11, 2013 at 8:55
  • Just start using Express anyway... Commented Nov 11, 2013 at 9:54

1 Answer 1

1

Before answering your question...

Your question aims at serving static web content.

You should install 'express' (a node module based on famous 'connect', which can be used for this as well but lacks other features) and configure it to serve your file from a static directory:

var express = require('express');
var app = express.createServer();

/* configure your static directory */
app.configure(function(){
  app.use(express.static(__dirname + '/static'));
});

/* on request, send index.html */
app.get('/', function(req, res){
   res.sendfile(__dirname + '/index.html');
});

app.listen(3000); 

Now that you have express installed, take a look at Jade.

You can then process received requests and dynamically serve content. That's state of the art - serving pre-coded html is 90's-style.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.