Missing Node Methods? - node.js

Working with Node 12.16.1 I can't seem to call the createServer method from the http module in IntelliJ. Also, I'm a noob (2nd post on stackoverflow) so I'm sure there is something really basic I'm missing. The first step toward learning from my mistakes is identifying them so your help is appreciated.
I know createServer is no longer used in Express 4, but it still shows as being a current method for the http Node 12.16.1 documentation online:
https://nodejs.org/dist/latest-v12.x/docs/api/http.html
https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_http_createserver_options_requestlistener
So when I start a file with
var http = require('http');
I figure I should be able to see createServer as an available method when I enter:
http.
I know there are other ways to create a server using express 4 and coding a function, so I'm not trying to figure out how to enable a server. I want to better understand the behavior of modules and methods and installing them. I have run into "missing methods" from node a couple of times and have usually found a work around.
This time I want to better understand the reason I'm not finding the method in my IDE. Is it deprecated? Is there something I need to configure differently? I have tried
npm install http
a couple of times and still don't find the method. The module call is recognized and numerous methods are available. Again I'm a noob and just looking to learn / understand this behavior. Any help is appreciated.
Thanks

With require it won't popup with intellisense, only if you use ES6 import syntax like this. So basically it will work with require too, but with import you can use only those parts which are needed for you. Take a look at here: The difference between "require(x)" and "import x"
import http, { createServer } from 'http';
http.createServer()

Related

Express Js Router methods

I am new to expressjs and nodejs .I have found few methods in express router using visual studio code vs intellisense .For few of them i didn't found any documentation regarding below methods ,how exactly it can be used in real world like get,post,put,patch .So i thought to ask in stackoverflow.It might help everyone who are looking similar to this
const router = express.Router()
router.options
router.head
router.checkout
router.connect
router.copy
router.lock
router.merge
router.mkactivity
router.mkcol
router.move
router.'m-search'
router.notify
router.propfind
router.proppatch
router.purge
router.report
router.search
router.subscribe
router.trace
router.unlock
router.unsubscribe
router.apply
router.bind
router.arguments
router.caller
router.call
Please see: https://expressjs.com/en/api.html#routing-methods
Express Routing methods
Express supports the following routing methods corresponding to the HTTP methods of the same names:
checkout copy delete get head lock merge mkactivity mkcol move m-search notify options patch post purge put report search subscribe trace unlock unsubscribe
The API documentation has explicit entries only for the most popular HTTP methods app.get(), app.post(), app.put(), and app.delete(). However, the other methods listed above work in exactly the same way.

How does Koa help avoid "monkey patching" and how "Hapi" or "Express" don't do the same?

I have hard times understanding why people preach Koa as solving the "monkey patching" problem (whereas one needs to modify prepackaged code). (see https://www.quora.com/Should-I-learn-Express-js-or-Koa-js-for-node/answer/Yvan-Scher?share=1 or http://blog.onclickinnovations.com/koa-js/).
How is Koa special in that regards? How isn't Hapi or Express the same in that regards?
Having done Koa for 2 years, and some express.js recently, I ran into 1 big example of this.
Say you have a controller that emits a response, and you want to intercept that response and do something with it (e.g.: gzip it, or convert it to some other format).
This works easily natively with koa because you can just do something like this:
function myMw(ctx, next) {
await next();
ctx.response.body = gzip(ctx.response.body);
}
The above is a fictional example, but you get the idea.
With express your code for this looks like absolute garbage. Easy to see in the express gzip middleware:
https://github.com/expressjs/compression/blob/master/index.js
This has to do with the fact that express middlewares provide direct access to the HTTP socket for writing responses (with send()).
I'm suspecting this is where this sentiment comes from. Frankly I don't understand why people still use Express. Mostly habitual and the vast amounts of tutorials I reckon. Express was great, but it's painful today.

All requests via Proxy in Node-Webkit

Is there a way to force Node-webkit to use proxied settings for all requests ? I know there is an API solution using setProxyConfig() but this doesn't work with authenticated proxy, the prompt shows up to login but crash when submitted...
So I tried to use node request module, and it works fine :
var request=require('request');
var proxy = request.defaults({'proxy':'http://login:pwd#proxy:port'});
But now my problem is to tell node-webkit to use this for each request.
Any solutions ?
I'm quite new using node and node-webkit so maybe there is a better way to do that.
Thanks a lot !
You may try
process.env.http_proxy = 'http://login:pwd#proxy:port'
It will work with request lib, also should impact other requests from node-webkit (assets, ajax)
But may be other libraries don't use environment proxy settings. If that doesn't work you can try https://www.npmjs.com/package/global-tunnel
Not sure if this is solved, but i came up with a simple solution:
i wrote a simple wrapper for the request library
https://gist.github.com/jodevsa/7dd9662b8244359fa0d7626ae7c9bd69
all you have to do is ,
Head to $PROJECT_DIR/node_modules/request/
Rename index.js to core.js
Create a new file called index.js
Copy code content from the above gist link to index.js
Change proxyLoc value to you'r preferred proxy
If you decided to disable proxy , just change value of ON variable to false inside index.js
Cheers :D

Stuck on getting mongoose, node and express to play nice together

I am following along with a node.js and express tutorial at DailyJS and have already hit a wall.
In the tutorial it says to do this:
mongoose = require('mongoose').Mongoose
db = mongoose.connect('mongodb://localhost/nodepad')
but beforehand he was talking about the code inside the app.js file, so I assume he means for the above code to go inside app.js but I don't know. At this point I download the nodepad from github to see where he actually put the mongoose code, and I can't find the above code anywhere in the entire application!
So I am at a loss. Things are already confusing because a default skeleton site made by express is different in many ways to the tutorial, because express has changed a lot in the 2 years since the tutorial was written.
So i'm trying to follow along but I just get really stuck where he says write some code but gives no indication where this code is meant to go.
And afterwards the tutorial says to add a models.js file, and I wonder where this goes, inside the directory perhaps?
(I also understand the above code is outdated and the correct code can be found here, the problem is not the code but where in express its meant to be placed)
Please give me some guidance.
Thanks
Both of those go in app.js. I can give you some more details. I use mongoose and express all the time. I don't use var mongoose = require('mongoose').Mongoose All I use is var mongoose = require('mongoose')
Let me know if you need any further help.

Express & Socket.io Route parroting/copying/sharing

I'm working with expressjs and socket.io in nodejs. I'm looking into assign identical route handlers to requests made in either HTTP or via websockets/socket.io.
For instance:
var responder = function(req, res){
req.params //<-- {id: 'something...'}
}
app.get('/foo/:id', responder);
io.on('/foo/:id', responder);
socket.io doesn't appear to have this type of routing functionality. Does anyone know of a library/module to help with this?
There are several options.
If you'd like to keep using express, check out express.io.
If you don't mind using something a bit different, sails lets you do this sort of thing as well.
(Update: sails now uses express too)
Both have been used in production successfully.
Note that routing is also pretty simple to implement on your own. If you check out how express do it I'm sure you'll be able to figure out a slim implementation that would match you needs.
Good luck! Let me know what you ended up using and how it worked for you.

Resources