JSON + Node.js - 意外的标识符

时间:2012-10-02 18:42:42

标签: javascript html json node.js

我正在为涉及json和node.js的大学课程工作。我遇到了一个错误,我似乎无法弄清楚我做错了什么。

这是错误:

/home/ubuntu/node_stuff/node_json2/requestHandlers.js:22
'<li>'appts.appointments.appt1.busName'</li>'+
      ^^^^^
SyntaxError: Unexpected identifier
at Module._compile (module.js:437:25)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/home/ubuntu/node_stuff/node_json2/index.js:3:23)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)

以下是我正在使用的文件:

appts.js

module.exports = {appointments: {
debug: "on",
appt1: {
    busName: "Asheville Eye Associates",
address: {
    street: "8 Medical Park Drive",
    city: "Asheville",
    state: "NC",
    zip: "28803"
},
phone: "828-258-1586",
apptDate: "October 5 2012",
dotw: "Friday",
apptTime: "9:00 am"
},
appt2: {
    busName: "Asheville Dental Care",
    address: {
            street: "10 Yorkshire Street",
            city: "Asheville",
            state: "NC",
            zip: "28803"
    },
    phone: "828-274-3882",
    apptDate: "October 22 2012",
dotw: "Monday",
    apptTime: "10:30 am"
},
appt3: {
    busName: "Asheville Cardiology",
    address: {
            street: "5 Vanderbilt Park Drive",
            city: "Asheville",
            state: "NC",
            zip: "28803"
    },
    phone: "828-274-6000",
    apptDate: "November 14 2012",
dotw: "Wednesday",
    apptTime: "9:30 am"
},

}};

requestHandlers.js

var querystring = require("querystring"),
appts = require("./appts"),
fs = require("fs");

function home(response, postData) {
console.log("Appointments have been parsed from JSON");
console.log("Request handler 'start' was called.");
console.log("stringify appts");
appts = JSON.stringify(appts);
console.log("parse appts");
appts = JSON.parse(appts);
console.log(appts.appointments);

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Appointment 1</h1>'+
'<ul>'+
    '<li>'appts.appointments.appt1.busName'</li>'+
    '<li>'appts.appointments.appt1.address.street'</li>'+
    '<li>'appts.appointments.appt1.address.city + " " + appts.appointments.appt1.address.state + " " + appts.appointments.appt1.address.zip'</li>'+
    '<li>'appts.appointments.appt1.phone'</li>'+
    '<li>'appts.appointments.appt1.apptDate'</li>'+
    '<li>'appts.appointments.appt1.dotw</li>'+
    '<li>'appts.appointments.appt1.apptTime'</li>'+    
'</ul>'+
'<h1>Appointment 2</h1>'+
'<ul>'+
    '<li>'appts.appointments.appt2.busName'</li>'+
    '<li>'appts.appointments.appt2.address.street'</li>'+
    '<li>'appts.appointments.appt2.address.city + " " + appts.appointments.appt2.address.state + " " + appts.appointments.appt2.address.zip'</li>'+
    '<li>'appts.appointments.appt2.phone'</li>'+
    '<li>'appts.appointments.appt2.apptDate'</li>'+
    '<li>'appts.appointments.appt2.dotw'</li>'+
    '<li>'appts.appointments.appt2.apptTime'</li>'+
'</ul>'+
'<h1>Appointment 3</h1>'+
'<ul>'+
    '<li>'appts.appointments.appt3.busName'</li>'+
    '<li>'appts.appointments.appt3.address.street'</li>'+
    '<li>'appts.appointments.appt3.address.city + " " + appts.appointments.appt3.address.state + " " + appts.appointments.appt3.address.zip'</li>'+
    '<li>'appts.appointments.appt3.phone'</li>'+
    '<li>'appts.appointments.appt3.apptDate'</li>'+
    '<li>'appts.appointments.appt3.dotw'</li>'+
    '<li>'appts.appointments.appt3.apptTime'</li>'+
'</ul>'+
'</body>'+
'</html>';

response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}

exports.home = home;

我确信我的问题很简单,但这是我第一次从另一个文件中提取对象。我感谢你们提供的任何帮助,谢谢。

1 个答案:

答案 0 :(得分:2)

您忘记了运营商(+之后可能'<li>')。但是,您可能有兴趣使用some templating engine instead of writing code yourself