Get started in Node.js is fun and easy enough nowadays. But setting up a Node server can still be convoluted if you just need a simple hello world type of server.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Often you need express and know which API to call on response object to send the appropriate content type. `res.send` or `res.sendStatus`, ha? Would it be much nicer if the server just return something, like a function? HTTP server is meant to be stateless and stateless reminds us of pure functions.
Just returns
// server.js
export default async function (req) {
return 'Hello, World!';
}
What if I told you this is my server, and I can run it directly and receive “Hello, World!” every time I curl it? Simple right? And since we are using async functions, async data flow can be handled with promise as well:
// server.js
export default async function (req) {
const words = await Promise.resolve('Hello, World!');
return words;
}
This is why I wrote fun-http and want to have some simple fun in Node.js.
$ npm i -g fun-http
$ fun-http server.js
$ curl -i localhost:3000
HTTP/1.1 200 OK
Date: Sun, 12 Jun 2016 22:12:41 GMT
Connection: keep-alive
Content-Length: 13
Hello, World!
Shared this if you like it. It will make me know you like my idea, so I’m motivated to make it better :)