I am newbie to Node.js, I am Designing a basic website using Node.js,HTML,CSS,JS.
In My Application After calling localhost:4000/ on my local server i am able to get homepage. In my home page I have a contactus Tab. When I click on that it should redirect to contactus.html page. But when i run it on my server it is showing Cannot GET /contactus.html.
This is my server:
var fs=require('fs');
var express=require('express');
var app=express();
app.use(express.static(__dirname + '/static'));
app.get('/', function(req,res) {
data= fs.readFile('/home/swift-03/WebstormProjects/website/static/HTML/home.html', function (err, data) {
res.setHeader('Content-Type', 'text/html');
res.send(data);
});
});
app.listen(4000);
and my Html page is:
<!DOCTYPE html>
<html>
<head>
<script>
function contactus() {
document.location="contactus.html";
}
</script>
</head>
<body>
<div class="pos_right">
<ul class="div">
<li><a href="#" onclick="contactus()">Contact</a></li>
</ul>
</div>
</body>
</html>
this is my directory structure:
Thanks in advance!!!
I am newbie to Node.js, I am Designing a basic website using Node.js,HTML,CSS,JS.
In My Application After calling localhost:4000/ on my local server i am able to get homepage. In my home page I have a contactus Tab. When I click on that it should redirect to contactus.html page. But when i run it on my server it is showing Cannot GET /contactus.html.
This is my server:
var fs=require('fs');
var express=require('express');
var app=express();
app.use(express.static(__dirname + '/static'));
app.get('/', function(req,res) {
data= fs.readFile('/home/swift-03/WebstormProjects/website/static/HTML/home.html', function (err, data) {
res.setHeader('Content-Type', 'text/html');
res.send(data);
});
});
app.listen(4000);
and my Html page is:
<!DOCTYPE html>
<html>
<head>
<script>
function contactus() {
document.location="contactus.html";
}
</script>
</head>
<body>
<div class="pos_right">
<ul class="div">
<li><a href="#" onclick="contactus()">Contact</a></li>
</ul>
</div>
</body>
</html>
this is my directory structure:
Thanks in advance!!!
Share Improve this question asked Nov 6, 2014 at 7:00 MAKMAK 3902 gold badges8 silver badges25 bronze badges 5-
Try just
<li><a href="contactus.html" >Contact</a></li>
– Paramore Commented Nov 6, 2014 at 7:12 - No,It is still showing same error. I have to handle it through Javascript. But i don't know how to handle it.. The problem is server is unable to access the contactus.html page.thankyou for responding – MAK Commented Nov 6, 2014 at 7:17
- Since you use express, just read express's API before you ask such a question. – creeper Commented Nov 6, 2014 at 7:19
-
app.use(express.static('/static', __dirname + '/static'));
All your files are in static directory, so it is not necessary to create routes. – Paramore Commented Nov 6, 2014 at 7:20 - @Paramore thank you for your response...i got the answer..I need to add routes for html page also.. – MAK Commented Nov 6, 2014 at 7:32
2 Answers
Reset to default 5You have not set the route for contactus.html . Add another get entry for that.
app.get('/contactus.html', function(req,res) {
data= fs.readFile('/home/swift-03/WebstormProjects/website/static/HTML/contactus.html', function (err, data) {
res.setHeader('Content-Type', 'text/html');
res.send(data);
});
If you want to set routes dynamically. You may set the route in the following way. Then you don't have to add multiple entries for each route. But be carefully about the security as user can access any file inside the directory..
app.get('/*', function(req,res) {
data= fs.readFile('/home/swift-03/WebstormProjects/website/static/HTML/' + req.url, function (err, data) {
res.setHeader('Content-Type', 'text/html');
res.send(data);
});
HTML Firstone, you need add the link in your page:
<li><a href="contactus.html" >Contact</a></li>
Replacing this line:
<a href="#" onclick="contactus()">Contact</a></li>
Your server
app.get('/contactus.html', function(req,res) {
data= fs.readFile('/home/swift-03/WebstormProjects/website/static/HTML/contactus.html', function (err, data) {
//Other information required...
res.setHeader('Content-Type', 'text/html');
res.send(data);
});
You need especify as first parameter in your app.get
request (in this case contactus.html)