我试图写一个简单的网页。它基于2个html和一个index.js。我使用nodejs并表示路由。当我用
启动时node index.js
它似乎工作正常,直到我到浏览器并转到
本地主机:8080 /
然后浏览器告诉我
无法获得/
终端或Firefox和Chrome的控制台中都没有显示错误。
以下是代码:
index.js
var express=require('express');
var app=express();
app.use(express.static(__dirname+'/public'));
app.get("/", function(req, res){
res.sendFile(__dirname+"main.html");
});
app.get("/choice/:choice/travel/:travel/name/:name", function(req, res){
res.sendFile("result.html", {
choice : req.params.choice,
travel : req.params.travel,
name : req.params.name
});
});
express().listen((process.env.PORT || 8080), function(){
console.log("Server running at: "+(process.env.PORT || 8080));
});
main.html中
<!DOCTYPE html>
<html>
<head>
<title>Survey Norway</title>
</head>
<body>
<h1>Survey Norway</h1>
<p>The survey is anonymous unless you insert your name<p>
<h2>Would you come to Norway this summer?</h2>
<form action="">
<input type="radio" name="choice" value="1" onclick="showMore()">Yes<br>
<input type="radio" name="choice" value="0" onclick="hide()">No
</form>
<div id="mytext" style="display: none;">
<h2>Which one would you choose?</h2>
<form action="">
<input type="radio" name="travel" value="1">Rent a car and travel Europe<br>
<input type="radio" name="travel" value="2">Fly. <br>
<input type="radio" name="travel" value="0">Doesn't matter.
</form>
</div>
<p>If you want insert yor name here:.</p>
<input type="text" name="name">
<center>
<button onclick="send_()">Send</button>
</center>
</body>
</html>
<script type="text/javascript">
function showMore (){
document.getElementById("mytext").style.display="block";
}
function hide (){
document.getElementById("mytext").style.display="none";
}
function send_(){
var choice=document.querySelector('input[name = "choice"]:checked').value;
var viaggio=0;
if (choice==1){
viaggio=document.querySelector('input[name = "travel"]:checked').value;
}
var nome=document.querySelector('input[name = "name"]').value;
console.log("choice: "+choice+"\ntravel: "+travel+"\nname: "+name);
}
</script>
谁能告诉我我做错了什么?
答案 0 :(得分:0)
改变这个:
CS1061
到此:
express().listen(...)
您正在创建一个新的快速实例并使用该实例进行侦听,这与您放置路由的实例不同。因此,您实际创建和启动的服务器上没有定义路由。