假设我正在编写一个带有服务器和客户端的Web应用程序.
node-static是提供静态的JavaScript/HTML文件的应用程序.我希望能够彼此独立地单独部署它们 - 或者两者同时部署它们.
以下是我设想的目录结构:
/my-app
app.js
/server
server.js
/client
client.js
Run Code Online (Sandbox Code Playgroud)
我希望能够以3种不同的方式运行它:
在某个端口(比如3000)上运行服务器(API):
my-app/server> node server.js
...Server listening on localhost:3000/api
Run Code Online (Sandbox Code Playgroud)只运行客户端(即从/ client目录提供静态文件):
my-app/client> node client.js
...Server listening on localhost:4000/client
Run Code Online (Sandbox Code Playgroud)在同一端口上运行服务器和客户端(由单个node.js实例):
my-app> node app.js
...Server listening on localhost:5000
Run Code Online (Sandbox Code Playgroud)这是否可能在节点中,配置它的正确方法是什么?
我开始如下:
/////////////
// server.js
/////////////
// Run the server if this file is run as script
if(module.parent){
app.listen("3000/client")
}
/////////////
// client.js
/////////////
var static = require('node-static');
var file = new(static.Server)('.');
var …Run Code Online (Sandbox Code Playgroud) 为什么当我在Haskell中使用范围时,这有效:
[LT .. GT]
Run Code Online (Sandbox Code Playgroud)
但这不是:
[LT..GT]
Run Code Online (Sandbox Code Playgroud)
这个神秘的错误意味着什么:
<interactive>:1:2:
Failed to load interface for `LT':
Use -v to see a list of the files searched for.
<interactive>:1:2:
A section must be enclosed in parentheses thus: (`LT..` GT)
Run Code Online (Sandbox Code Playgroud)
但是,当我使用Ints时,第二种形式(没有空格)有效:
[1..3]
Run Code Online (Sandbox Code Playgroud) scala中的所有"服务器"示例都使用actor,reactor等...
有人可以告诉我如何编写一个死的简单回显服务器和客户端,就像下面的服务器和客户端的 python示例:
# A simple echo server
import socket
host = ''
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
while 1:
client, address = s.accept()
data = client.recv(size)
if data:
client.send(data)
client.close()
Run Code Online (Sandbox Code Playgroud)
# A simple echo client
import socket
host = 'localhost'
port = 50000
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.send('Hello, world')
data = s.recv(size)
s.close()
print 'Received:', data
Run Code Online (Sandbox Code Playgroud) Java不是我的主要编程语言,所以我可能会问这个显而易见的问题.
但是在Java中是否有一个简单的文件处理库,比如在python中?
例如,我只想说:
File f = Open('file.txt', 'w')
for(String line:f){
//do something with the line from file
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
更新:嗯,stackoverflow自动接受了一个奇怪的答案.它与我放置的赏金有关 - 所以如果你想看到其他答案,只需向下滚动!
使用Java LinkedList时,如何找出元素的下一个或上一个关系?
我的意思是,在常规链表中我会做这样的事情:
Node node1 = new Node();
Node node2 = new Node();
LinkedList list = new LinkedList();
list.add(node1);
list.add(node2);
//then my node1 will know who it's next is:
assertEquals(node2, node1.next());
Run Code Online (Sandbox Code Playgroud)
其中Node是我自己的数据/对象容器.
但是在Java的LinkedList中,数据似乎没有被修改.那么我如何才能真正找出"下一个"(或双链表中的"前一个")元素是谁?
我试图用匹配替换我的isInstanceOf检查,但它不起作用.
在我的方法中,我检查树节点 - 如果它是叶子 - 我想立即将它返回到Vector中,如果不是 - 我继续使用该方法.
所以最初我有:
//code here
if (common.isInstanceOf[LeafNode]) {
return Vector(common.asInstanceOf[LeafNode].data)
}
//code here
Run Code Online (Sandbox Code Playgroud)
然后我试着用以下代替:
//code here
common match {
case leaf: LeafNode => return Vector(leaf.data)
}
//code here
Run Code Online (Sandbox Code Playgroud)
但我得到了scala.MatchError.
我正在阅读Scala编程,我不理解以下句子(pdf第112页):
每个单例对象都实现为从静态变量引用的合成类的实例,因此它们具有与Java静态相同的初始化语义.
这是否意味着如果我在scala中有单例FooBar,编译器将创建一个名为FooBar $的类?
另外作者的意思是"从静态变量引用"?是否有一个隐藏的静态变量,某处持有对某些FooBar $类的引用?
我感谢任何帮助.