1

I'm playing around with Nimrod a bit, and I've tried the HTTPServer module. I first thought it didn't work as when I tried to go to the web page in Safari, my standard browser, it gave an error. There error was something like this: (sorry if it isn't exactly right, it's translated from dutch)

Safari can't open this page Safari cannot open page localhost:5000, as the network connection has been lost unexpectedly

But, when I try to open the page in Chrome or Firefox, it works just fine

My Nimrod code:

import strutils, sockets, httpserver

var counter = 0
proc handleRequest(client: TSocket, path, query: string): bool {.procvar.} =
  inc(counter)
  client.send("Hello for the $#th time." % $counter & wwwNL)
  return false # do not stop processing

run(handleRequest, TPort(5000))
4

1 回答 1

2

似乎 client.send 直接写入线路 - 没有添加 HTTP 状态行或任何标头。您可以通过至少添加状态行使其工作:

import strutils, sockets, httpserver

var counter = 0
proc handleRequest(client: TSocket, path, query: string): bool {.procvar.} =
  inc(counter)
  client.send("HTTP/1.0 200 OK\r\n\r\nHello for the $#th time." % $counter & wwwNL)
  return false # do not stop processing

run(handleRequest, TPort(5000))
于 2014-11-02T19:27:38.043 回答