Skip to content

Instantly share code, notes, and snippets.

@vinipsmaker
Last active March 8, 2021 14:48
Show Gist options
  • Save vinipsmaker/719219ca4fd2609808f81f85058493d5 to your computer and use it in GitHub Desktop.
Save vinipsmaker/719219ca4fd2609808f81f85058493d5 to your computer and use it in GitHub Desktop.
const http = require('http');
const a = {
'content-type': 'text/plain'
};
const b = 'Hello, World!\n';
http.createServer((request, response) => {
response.writeHead(200, a);
response.write(b);
response.end();
}).listen(8080);
local ip = require 'ip'
local http = require 'http'
local acceptor = ip.tcp.acceptor.new()
acceptor:open('v4')
acceptor:set_option('reuse_address', true)
if not pcall(function() acceptor:bind(ip.address.loopback_v4(), 8080) end) then
acceptor:bind(ip.address.loopback_v4(), 0)
end
print('Listening on ' .. tostring(acceptor.local_address) .. ':' ..
acceptor.local_port)
acceptor:listen()
local res = http.response.new()
res.status = 200
res.reason = 'OK'
res.headers['content-type'] = 'text/plain'
res.body = 'Hello, World!\n'
while true do
local sock = http.socket.new(acceptor:accept())
spawn(function() pcall(function()
local req = http.request.new()
while true do
sock:read_request(req)
while sock.read_state ~= 'finished' do
req.body = nil --< discard unused data
sock:read_some(req)
end
sock:write_response(res)
end
end) end):detach()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment