Last active
June 26, 2020 22:54
-
-
Save ctaggart/0ab58535e8e57f4d6ef653a6c8a07c3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod logging; | |
use node::http::{create_server, IncomingMessage, RequestListener, ServerResponse}; | |
use wasm_bindgen::prelude::*; | |
#[wasm_bindgen] | |
pub fn run() { | |
// based on https://nodejs.org/en/docs/guides/getting-started-guide/ | |
let default_port = 3000.; | |
let port = match node::PROCESS.env().get("PORT") { | |
Some(port) => port.parse::<f64>().unwrap_or(default_port), | |
None => default_port, | |
}; | |
let listener: RequestListener = Closure::wrap(Box::new(move |_req: IncomingMessage, res: ServerResponse| { | |
res.set_status_code(200.); | |
res.as_outgoing_message() | |
.set_header("Content-Type", &JsValue::from_str("test-plain")); | |
res.as_stream_writable().end(&JsValue::from_str("Hello World from WebAssembly")); | |
})); | |
let server = create_server(&listener); | |
listener.forget(); | |
log!("server starting on port {}", port); | |
server.as_net_net_server().listen(Some(port), None, None, &JsValue::UNDEFINED); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment