我是js的新手,所以对我很轻松。 我的主要目标是从Message.js(一个名为messages的数组)中获取参数,并在服务器中使用它们。 对于这部分只是在屏幕上打印它们会做。
给帮助者tnx! :)我的Message.js文件如下所示:
$(document).ready(function() {
var messages = [
{
name: 'I am Message number one', //a js field in an object(message in this case) and it's value after the semicolon
texts: [ //an array of 3 strings in js
'mary had a little lamb',
'hail hydra',
'spider-man rocks'
],
images: [ //an array of 2 images in js
'https://fb-s-c-a.akamaihd.net/h-ak-xat1/v/t1.0-9/10156120_630371003705480_8529510393733631385_n.png?oh=8412d9c3b69d39b6030d66f9709e1e1e&oe=58EEA4F2&__gda__=1492580526_84a87f898177ec43770cf3a5317fdb31',
'https://fb-s-a-a.akamaihd.net/h-ak-xtf1/v/t1.0-9/10268685_630867936989120_785222708436272994_n.jpg?oh=c15c538b5385843ad0b44affda0f378c&oe=58B9EB5F&__gda__=1488430186_6c7c227a21e77492f092dc999834157d'
],
times: [ //array of vars initialized in Dates kind values
{
fromDate: new Date(2016, 0, 1), //jan is 0, feb is 1, and so on...
toDate: new Date(2016, 11, 31),
fromTime: '09:00',
toTime: '22:53',
days: [0, 1, 5] // sunday starts with a 0 then monday is 1 and so on...
}
]
}, //until here this is a one object of 'message'
{
name: 'I am Message number two', //a js field in an object(message in this case) and it's value after the semicolon
texts: [
'soon in theaters', //an array of 4 strings in js
'the lion king is Simba',
'tiom&pumba rules',
'spider-man is awesome!'
]
}];
}
我不知道该怎么做服务器的部分 尝试过这样的事情:
var http = require('http');
var port = 8080;
var fs = require("fs");
const querystring = require("querystring");
function onRequest(request, response){
console.log("user made a request " + request.url);
response.writeHead(200);
response.write(messages[0].name);
response.write("here is your data");
response.toString(message[0]);
response.end();
}
http.createServer(onRequest).listen(port);
答案 0 :(得分:0)
在Message.js
更改:
var messages = [ ...
为:
module.exports = [ ...
并使用它:
var messages = require('./Message.js');
在您应用的其他一些文件中。
如果该文件不在同一目录中,则可能需要使用:
var messages = require('../Message.js');
或:
var messages = require('../lib/Message.js');
或类似的内容,具体取决于您希望require
所在文件的角度所在的位置。
答案 1 :(得分:0)
在你的Message.js中添加
exports.messages= messages; //add this after your messages array declaration
和你的服务器......
var myMessages= require('./Message.js');
var messages= myMessages.messages;