2016-12-27 38 views
0

我是js的新手,所以对我来说很简单。 我的主要目标是从Message.js(一个名为消息的数组)中获取参数并在服务器中使用它们。 这部分只需在屏幕上打印即可。如何从node.js服务器中的.js获取和使用变量和参数?

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

你只谈论服务器端,或者你想从客户端发送此数组服务器? – 2016-12-27 17:33:16

+0

Did [my answer](https://stackoverflow.com/questions/41349459/how-to-get-and-use-variables-and-parameters-from-js-in-node-js-server/41349513#41349513 )下面回答你的问题?任何意见? – rsp

+0

客户端到服务器。主要目标是显示一个网站,显示几条消息,而不需要重新加载,所以对于这个逻辑我使用setInterval() 来显示消息从一个HTML文件,呈现和显示这些消息。现在我正在尝试扩展该项目,并使用服务器从.js文件获取数据,并将其发送给客户端进行渲染并显示。 –

回答

0

Message.js变更:

var messages = [ ... 

到:

module.exports = [ ... 

而且随着使用它:

var messages = require('./Message.js'); 
在您的应用程序的一些其他文件

如果没有该文件在同一目录中,那么你可能需要使用:

var messages = require('../Message.js'); 

或:

var messages = require('../lib/Message.js'); 

或类似的东西,取决于它是从点位置看文件的要require进去。

0

在你Message.js添加

exports.messages= messages; //add this after your messages array declaration 

,并在您的服务器......

var myMessages= require('./Message.js'); 

var messages= myMessages.messages; 
相关问题