Express Vhost 'argument handle is required' - node.js

Firstly, found a couple of similar question on here but no duplicates, I think my situation is slightly different.
Trying to get a website and associated API working on Express using vhost for subdomians.
Here is my folder structure
/api
api.js
/server
website.js
server.js
My server.js
const vhost = require('vhost');
const express = require('express');
const app = express();
app.use(vhost('localhost', require('./server/website.js').app));
app.use(vhost('api.localhost', require('./api/api.js').app));
app.listen(1337, () => {});
My api.js
const express = require('express');
const app = express();
app.get('/', function(req, res){
res.send({ hello: 'world' });
});
module.exports = app;
Initially my path to api.js was wrong and I got a not found error so now I know my path is right but now I get the error "Typeerror: argument handle is required" whatever I do.
Any help would really be appreciated.

Your exporting the app already. So it is not necessary to add .app to the end of your require.
It should be:
app.use(vhost('localhost', require('./server/website')));
app.use(vhost('api.localhost', require('./api/api')));
Hope that helps.

here's what I did:
//used the api.localhost as the subdomain url
//it requires another express app.js to work
//the other express app must -> module.exports = app;
const vhost = require('vhost');
const app = express();
app.use(vhost('api.localhost', require('./api/app')));

Related

Using mongoose in multiple routes with express

I have set up my code in server.js which worked fine. I am now trying to clean up the server.js code by splitting the code into different routes.
I am using the express.Router to link up the files but i am struggling to understand why my post route is braking.
In the server.js file
// dependencies
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyparser.urlencoded({extended: true}));
app.set('view engine', 'ejs');
// routes
constpurchaseOrders = require("./routes/purchaseOrders");
app.use("/purchaseOrders", purchaseOrders);
// listen
app.listen(3000, function(){
console.log("Server started on port 3000");
});
In the purchaseOrders.js file
// dependencies
const express = require("express");
let router = express.Router();
const mongoose = require("Mongoose");
const connection = require("../routes/mongoose.js");
const Po = require("../routes/schemas.js:);
// post route
router.post("/addpo, function(req.res){
//long code lines here
po.save();
res.redirect("/");
});
module.exports = router;
So with this route, i have the following end point on the route.get:
10.0.0.15:3000/purchaseOrders/addpo
Now the problem comes in when i submit the form, it returns "cannot POST /addpo" and the end point shows:
10.0.0.15:3000/addpo
On my ejs file i have set the form up as follows:
<form action="/addpo" method="post">
On the mongoose.js connection i export as follows:
module.exports = mongoose;
On the schemas.js i export:
module.exports = Po;
I know I did something wrong with the routing but i cannot figure our why.
Thanks alot :-)
if you need the /addpo path to work, change this on server.js:
app.use("/", purchaseOrders);
NOT
app.use("/purchaseOrders", purchaseOrders);
Other option
server.js:
app.use("/addpo", purchaseOrders);
purchaseOrders.js
router.post("/", function(req.res){
If you add a route before calling a router it will be added in front of it.
So, yo can use the route action="/addpo" in the html.
Express docs

Not able to find routes in Koa2, facing error TypeError: route.routes is not a function

I am working on Node Koa2 API. I am performing CRUD operations with Mongoose. When I am working with only one file ("app.js") its working fine. But when I am separating it into controllers, routes and model, it is showing following error: TypeError: route.routes is not a function in the app.js file. Thanks in Advance for the help.
Error Description:
import Koa from 'koa';
const BodyParser = require("koa-bodyparser");
const logger = require('koa-logger');
import router from './routes/index';
require('mongoose');
require('./config.js');
const app = new Koa();
// Use the bodyparser middlware
app.use(BodyParser());
app.use(logger());
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3001, () =>{
console.log('Server is running on port: 3001');
})
export default app;
wellcome to SO, change the fallowing in your code:
app.use(router.routes()) //replace for
app.use('/', router)
Hope it fixes the problem.
As per your issue for e.g. your index file path is (./routes/index).Then the code in app.js as follows:-
var index = require('./routes/index');
app.use('/',index);

Cannot /GET - What's wrong with this Express Server?

I'm creating a basic app to learn Express, but can't seem to set it up right. When I run the app I get a Cannot /GET error. The basic outline is something like this:
In the top directory -
var express = require('express');
var app = express();
var getWx = require('./incoming/getWx.js');
app.set('port', process.env.PORT || 1983);
app.use('/getWx', getWx);
app.listen(1983);
Then, in /incoming/getWx.js, I have:
var express = require('express');
var app = express();
var router = express.Router();
router.route('/')
.get(function(request, response) {
// do thing here
})
module.exports = router;
Anything stand out here as wrong? Trying to do this with a router as my app will end up with multiple files.
You get that error because you probably trying to access the path / ...
Which don't have any router set to handle it...
The router you set up handles the path /getWx
If you set stomething like this:
app.use('/', getWx);
The accessing path / will return something...

How to efficiently separate routing code

Dumb/Newb question...
I am learning/working on an API in Node / Express4 and I would like to break my routes out into another module. I have it working with the following code, but it seems awkward to me to keep re-using the require('express') statement... Is there a way to move more of the code from the routes.js file into server.js and still keep my .get and .post statements in the routes module? Thanks in advance!
server.js:
'use strict';
var express = require('express');
var routes = require('./routes');
var app = express();
app.use('/api', routes);
app.listen(3000, function() {
console.log('Listening);
});
routes.js
var express = require('express'); // how do I get rid of this line?
var router = express.Router(); // can I move this to server.js?
var apiRoute = router.route('');
apiRoute.get(function (req, res) {
res.send('api GET request received');
});
module.exports = router;
Your on the right track. Its actually cool to reuse the var express = require('express'); statement each time you need it. Importing, ( requiring ), modules is a cornerstone of modular development and allows you to maintain a separation of concerns with in the files of your project.
As far as modularly adding routes is concerned: The issue is that routes.js is misleading.
In order to modularly separate out your routes you should use several modules named <yourResource>.js. Those modules would contain all of the routing code as well as any other configuration or necessary functions. Then you would attach them in app.js with:
var apiRoute = router.route('/api');
apiRoute.use('/<yourResource', yourResourceRouter);
For example, if you had a resource bikes:
In app.js or even a module api.js:
var apiRoute = router.route('/api')
, bikeRoutes = require('./bikes');
apiRoute.use('/bikes', bikeRoutes);
Then in bike.js:
var express = require('express');
var router = express.Router();
var bikeRoutes = router.route('/');
bikeRoutes.get(function (req, res) {
res.send('api GET request received');
});
module.exports = bikeRoutes;
From there its easy to see that you can build many different resources and continually nest them.

express 4 router with external file

I have the following files
lib/pub
lib/pub/index.js
app.js
On App.js
I have:
// app.js
var express = require("express")
, app = express()
, router = express.Router()
;
...
router.use('/pub',require('./pub'));
and then on index.js
// pub/index.js
var express = require('express')
, router = express.Router()
;
console.log("file loaded successfully")
module.exports = function(){
router.get('/',function(req,res){
console.log("got the get request")
})
}
The problem I have when I do localhost/pub request, I never get the got the get request, no matter whatever I try to change the code around, trying to add pub to the path.
router.get('/',...
router.get('/pub',...
router.get('./pub,...
router.get('./',...
router.get('pub',...
etc...
None of those or any other silly way I have attempted work... I can never get the log to say yes I got the request...
What am I doing wrong ! (expressjs changes so frequently and radically, any web tutorials become redundant or any previous help others got)
(edited to reflect comments)
If you want to move your routes to an external file, use the following pattern:
app.js
var express = require('express');
var app = express();
require('./routes')(app);
routes.js
module.exports = function(app) {
app.get('/pub', function(req, res) {
console.log('got the get!');
res.end();
});
};

Resources