node.js http module http.createServer how does it work? - node.js

so i'm trying to understand how to use node.js module "http" http.createServer()
i wonder if it's possible to see how this function is defined? can i find the content of the definition? i wasn't able to find it

The Node.js source code is available on Github. For the JavaScript side of the API, take a look in the lib directory.
https://github.com/joyent/node/blob/master/lib/http.js#L61
exports.createServer = function(requestListener) {
return new Server(requestListener);
};
You can find the actual HTTP server JavaScript in lib/_http_server.js.

Related

what do you call those list in node.js site

I apologize to ask this question,I am still newbie to node.js and trying to learn on it..I am just confuse in http://nodejs.org/api/ are they all api or module? if we use this in our app.js
var http = require('http');
are calling we calling the api or the module ?please enlighten.are module and api are just the same ?
Thank you in advance.
The http api is the set of functions that the http module exposes, IE. through module.exports.

Using node.js libraries on front-end

How can I use the 'request' node.js module on the front-end?
normally I would retrieve it like so:
var request = require('request');
but this is not possible on the front-end since require is not recognized.
What is the best way to solve this?
To use node modules in the browser you can use a library called Browserify . This allows you to work with the common module pattern as well as the you can use this package browser-request to get the features of request module

Browserify the node.js http server

We created a simple js file, intending to find out if http.createServer works on client browser or not:
var http = require("http")
var server = http.createServer()
server.listen(9024, function () {
console.log("demo server listening on port 9024")
})
and embedded it into a html after browserify.
Display the html in chrome, unfortunately, it always fails on line 2 on http.createServer():
"Uncaught Type Error: undefined is not a function"
We also played around with "serve-browserify" a bit without success.
We have attempted the same thing on both chrome and firefox, and on Linux and Windows. All failed.
Searching through the web, there are quite a few examples for browserify http into the browser.
They all appear to be simple invocation of browserify. However we don't seem to be able to get the same good result.
Your help will be greately appreciated.
You can't use Node.js modules in the browser. All Browserify does is bundling CommonJS modules, it does not allow you to run server side code in the browser.

How can I pass extra options to Node in Meteor's HTTP.call()?

I'm getting an SSL error when doing an HTTP.get() call in Meteor, UNABLE_TO_VERIFY_LEAF_SIGNATURE.
The links above point to solutions involving Node parameters (for instance {rejectUnauthorized: false}), but it's unclear how to pass any of those to Meteor. I've tried HTTP.get(url, {rejectUnauthorized: false}) without luck.
It's now possible by passing npmRequestOptions to Meteor HTTP requests:
const requestOptions = {
npmRequestOptions: {
rejectUnauthorized: false
}
}
const result = HTTP.get(url, requestOptions)
I ended up creating a fork of Meteor's HTTP package, which just passes through options it doesn't know about. I think it's a sane thing to do (instead of discarding the options entirely), and I hope the Meteor team pulls the change into core.
The Atmosphere package is called http-more.
Looking at the source of the HTTP package (https://github.com/meteor/meteor/blob/devel/packages/http/httpcall_server.js#L75), I noticed that it isn't implemented using node's http class directly, but instead uses the request package and the options you can pass it (see line in above link) are limited. So I'm not sure this is currently possible.
Looking at the request package's request options (https://github.com/mikeal/request#requestoptions-callback) I wouldn't be sure how to enable the option you care about either.
BTW, if you are on the server, you can always use http(s) directly using Npm.require('https').

Redefining `require` on the client (but not on server) for code-sharing

I want to share a piece of coffeescript code between the server and the client on Express.
I linked the file from the server directory into the /public directory, so it can be loaded by the client.
It does have external dependencies, which I resolve statically for the client. To remove the error messages when the client tries to call require, I thought a simple conditional declaration would do.
console.log require
unless require?
require = (what) ->
console.info "this is the client, you asked me to load #{what}"
return {}
However, when run on the server, undefined will be printed and require will be overridden. The same happens for embedded Javascript:
`if( typeof require == "undefined" )
var require = function(what) {
console.info( "this is the client, you asked me to load "+what );
return {};
}
`
If I only run:
console.log require
on the server, it prints an object structure, as expected.
It seems that require is injected after at least the conditional has been evaluated and executed.
How can I override require safely, or what other paradigm might I use for sharing code between client and server?
I recommend using browserify for sharing code between the client and server. It allows you to write your javascript as you would for the server following the common js pattern, but provides a mechanism to build your module for client-side where require() works in the browser.
CoffeeScript is supported with browserify via a transform process. Coffeeify is one implementation of a transform implementation for CoffeeScript.

Resources