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

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.

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.

Missing Node Methods?

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()

How to fix "Express is not a constructor" Type Error when you deploy your app to heroku but app can't start?

I have deployed an app to heroku but it can't start because of the type error "express is not a constructor" in my index.js file.
Files can be found in https://github.com/Wachiye/blog.git
Express is just a factory function, not a constructor. So, you don't use new with. You would use it like this:
const express = require('express');
const app = express();
Shown right here in the doc.
In the future, please embed the relevant code portion into your question and format it appropriately. Stackoverflow has a whole bunch of reasons for wanting you to do it that way AND it will get you a faster answer. You get answers here faster when YOU make the question quick and easy to understand without going to external resources.

Import only once a plugin in hapijs and use it everywhere

I should use a plugin named hapi-mongoose-db-connector into my hapijs application. In the repository page the developers suggest the ways you can import correctly it. It says that the following way is the bad way:
# from the server
mongoose = server.pack.plugins['hapi-mongoose-db-connector'].mongoose
# or from a plugin
mongoose = plugin.plugins['hapi-mongoose-db-connector'].mongoose
and discourages using it. Instead he recommends to do in the following way:
You do nothing and just require mongoose in your plugins. As npm
requires are singletons (the code is loaded only once this works very
well)
but he doesn't show any examples. At this point I'm not pretty sure how to use it. I wouldn't call in every js files mongoose. I would call it once in my application somewhere and in my js files where I create models for the database, use it. Do you know any best practices in those cases?
Actually, first one is the hapi way doing this kind of thing.
But as the mongoose module is a singleton, that plugin just require mongoose and initialize it [1] after load that plugin into hapi, you can use mongoose in any file;
var mongoose = require("mongoose");

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