Error when using vhost in express - node.js

My code for server.js is:
var express = require('express');
var ENV = process.env['NODE_ENV'] || 'development';
var config = require('./config')[ENV];
// The express server to listen for both the clients
var main = express();
// Main application
main.use(express.vhost('vypics', require('./web_app/app').app))
// Example sub domain
main.use(express.vhost('android.vypics',require('./android_app/app').app));
main.listen(3000);
console.log('started on 3000');
when I am running node server.js I am getting the following error.
/Users/saransh2012/Developer/vypics/node_modules/express/node_modules/connect/lib/middleware/vhost.js:30
if (!server) throw new Error('vhost server required');
^
Error: vhost server required
at Function.vhost (/Users/saransh2012/Developer/vypics/node_modules/express/node_modules/connect/lib/middleware/vhost.js:30:22)
at Object.<anonymous> (/Users/saransh2012/Developer/vypics/server.js:8:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
Please help.....What Am i Doing wrong.

You'll get that error when the second argument to express.vhost is falsy.
My guess would be that either require('./web_app/app').app or require('./android_app/app').app (or both) is undefined.

The error was in the require('.web_app/app').app part as mentioned by robertklep.It was a very silly error on my part as i forgot to write the export for the app.
Sorry guys for such a silly error but sometimes those are the one very difficult to see.

Related

How to using eventSource on nodejs

I am new on nodejs
ı have a project for server sent events.
I am trying get datas with server sent events on console.
this is my code:
var source = new EventSource('https://sse.now.sh');
source.onmessage = function(e) {
console.log(e.data)
};
but when i try start project with node file.js :
ReferenceError: EventSource is not defined
at Object.<anonymous> (c:\Users\SYDNEY\Desktop\server-sent-events-demo\src\client\js\app.js:7:5)
at Module._compile (internal/modules/cjs/loader.js:953:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11
its just simple but i couldnt do anyting. should ı download anything about that ? bcz this script working on jsbin(website)
I solved this problem with add top of codes
var EventSource = require('eventsource')
var source = new EventSource('https://sse.now.sh');
source.onmessage = function(e) {
console.log(e.data)
};

nodemon - app crashed - waiting for file changes before starting

I just started learning Node.js. The idea of this small application is to use express and mongosse to store some users in a based cloud database (mongoDB via mlab).
I have two seperate files :
User.js (models/User.js)
var mongoose = require('mongoose');
module.exports = mongoose.model('User', {
email : string ,
pwd : string
});
server.js (dossier root)
var express = require('express')
var cors = require('cors')
var bparser = require('body-parser')
var mongoose = require('mongoose')
var User = require('./models/User.js')
var app = express()
app.use(cors())
app.use(bparser.json())
app.post('/register', (req,res) => {
userData = req.body;
var user = new User(userData);
user.save((err, result) => {
if(err) console.log('IL YA UNE ERREUR')
result.sendStatus(200);
})
})
mongoose.connect('mongodb://user:pwd#ds261755.mlab.com:61755/myapp', { useMongoClient: true } , (erreur) => {
if(!erreur)
console.log('Connexion etablie');
})
app.listen(3000)
When I execute : nodemon server.js, I get the error below:
D:\Bureau\MEAN\appBackend\models\User.js:5
email : string ,
^
ReferenceError: string is not defined
at Object.<anonymous> (D:\Bureau\MEAN\appBackend\models\User.js:5:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Bureau\MEAN\appBackend\server.js:6:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
[nodemon] app crashed - waiting for file changes before starting...
Do you have any idea about this error?
Mongoose expects you to specify types using the built-in constructor functions, which are named with capital letters, e.g. String, Number, Boolean, etc.
var mongoose = require('mongoose');
module.exports = mongoose.model('User', {
email : String ,
pwd : String
});
define
module.exports = mongoose.model('User', new mongoose.Schema({
email : 'string' ,
pwd : 'string'
})
});
There is no string variable in the code .
this message will be appeared always when there's any small mistake.
I also faced this when module.exports type as module.export,just missed 's' only.
I come across with such error and on my case it was syntax error in the node js file.check your code for error as nodemon listen to change and run if no error there.
This was the error that make app-crashed ....
console.log("this is author",cope["author"];
SyntaxError: missing ) after argument list
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19) After i correct the error [nodemon] restarting due to changes... [nodemon]
starting node server.js
That is it. Check for error in your code the nodemon automatically start the server.That is why nodemon is best rather than using node filename.js cz every time you have to start whenever you change the code.
The common reason is that you're trying to access the database from an IP that isn't whitelisted.
For my case, I was working on the same project on another PC. So, i had to whitelist the PC IP.
To do this, login to your cloud base mongodb, navigate to that your particular db project, and click on network access, add IP address and click on current ip address.
Also, make sure on your terminal, you run mongod to start your mongodb.
That solve for me
[nodemon] app crashed - waiting for file changes before starting...
You can fix this error with adding "start": "nodemon server.js" entry to your package.json file.
Test with this command:
npm install --save express-load
Many times we closed the PC and left the project running, when we used it again and ran the project again, that error appeared. I always solved it, restarting the pc. Everything worked perfectly.

Unable to run 'npm start' in Mac

I am trying to run the following command "npm start" however it throws the following error:
internal/net.js:17
throw new RangeError('"port" argument must be >= 0 and < 65536');
^
RangeError: "port" argument must be >= 0 and < 65536
at assertPort (internal/net.js:17:11)
at Server.listen (net.js:1389:5)
at EventEmitter.listen (/Users/keyurshah/fulljs/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/Users/keyurshah/fulljs/server.js:10:8)
at Module._compile (module.js:570:32)
at loader (/Users/keyurshah/fulljs/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/keyurshah/fulljs/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
[nodemon] app crashed - waiting for file changes before starting...
Check server.js line 10. Your port is probably specified there.
If not just declare it to be what you want right before the listen function.
From
app.listen(port, ...)
to
app.listen(8080, ...)
Otherwise you haven't shown any code not sure how we could help you with such limited information...
I recommend you start with more simple projects and get a feeling for how things work: http://expressjs.com/en/starter/hello-world.html
try to do this or change with any port number you like .listen(portNumber)
var http = require('http');
http.createServer( function (request, response) {
//your code
}).listen(8081);

Koa-pg Can't find module pg

Hey so I'm trying to get Nodejs Koa to talk to postgres using the Koa-pg module, but I keep getting a 'Can't find module pg' error.
I've tried to follow the koa-pg examples, but have come up short...so any advice would on how to progress would be appreciated.
If created my app.js file as follows:
var koa = require('koa');
var route = require('koa-route');
var koaPg = require('koa-pg');
var roads = require('./controllers/roads');
var app = module.exports = koa();
app.use(route.get('/roads/bbox/', roads.bbox));
app.listen(3000);
console.log('listening on port 3000');
And then created my controller file as follows:
var credentials = require('../credentials.js');
var environment = credentials.dev;
app.use(koaPg('postgres://' + environment.user + '#' + environment.host + ':' + environment.port + '/' + environment.database))
module.exports.bbox = function * bbox(next) {
var result = yield this.koaPg.db.client.queryPromise('SELECT now()')
console.log('result: ', result)
this.body = result.rows[0].now.toISOString()
};
But I'm getting the following error:
module.js:338
throw err;
^
Error: Cannot find module 'pg'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (c:\Users\User\Documents\restful_koa\node_modul
es\koa-pg\index.js:12:27)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
This is only a personal project but I'd love to understand where i'm going wrong.
Cheers
As mentioned in the comments:
You need to install the pg module via npm install pg or npm install pg --save if you want to save it to package.json.
The reason you need to do this is koa-pg has co-pg as a dependency so when you install the former the latter is also installed. But pg is not a dependency of co-pg and that is why you have to install it separately.

nodejs automatic url to folder mapping

I'm new to nodejs. What I want is to have several apps run at different context roots based on the directory structure. (all must be online at the same time)
For Example:
dir1
|____test1.js
|____test2.js
dir2
|____test1.js
|____test2.js
http://localhost:3000/dir1/test1
http://localhost:3000/dir1/test2
http://localhost:3000/dir2/test1
http://localhost:3000/dir2/test2
It's desired to automatically discover the scripts and create the end points.
I could not find such a feature in a test framework like mocha.
Can you please guide me with nodejs server url mapping?
Update:
I've tried this:
var express = require('express');
var app = express();
entrypoints = ["/a", "/b", "/c"]
entrypoints.forEach(function(val, index, arr){
app.get(val, function(req, res){
x = require("."+val+".js");
x.handle(req, res);
});
}
app.listen(3000);
where each a.js, b.js, c.js implements the handle but it fails; it does not like putting app.get inside a loop:
app.listen(3000);
^^^
SyntaxError: Unexpected identifier
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3

Resources