Hapi js - makemehapi Issue - node.js

Hi New Node and I am practising tutorial "makemehapi" In the third assignment I have receiving below error. Any one can point where I am doing wrong?
Regards,
Surya
------------ Code ---------------------
var Path = require('path');
var Hapi = require('hapi');
var Inert = require('inert');
var server = new Hapi.Server();
server.connection({
host: 'localhost',
port: Number(process.argv[2]|| 8080)
});
server.register(require(Inert),function(err){
if(err) throw err;
} )
server.route({
method:'GET',
path:"/index.html",
handler: {
file: "index.html"
}
})
server.start(function () {
console.log('Server running at:', server.info.uri);
});
---------------------- error--------------------------
assert.js:86
throw new assert.AssertionError({
^
AssertionError: path must be a string
at Module.require (module.js:364:3)
at require (module.js:384:17)`enter code here`
at Object.<anonymous> (/home/surya/Desktop/nodeschool/Hapi Practice/test/Lesson3.js:13:17)
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 Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
✗ Error connecting to http://localhost:18384: ECONNREFUSED
events.js:85
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)

You are requiring in Inert twice. Remove the require around the Inert variable in the following line:
server.register(require(Inert),function(err){
if(err) throw err;
});
To:
server.register(Inert,function(err){
if(err) throw err;
});

Related

How do I access the Error code from Node.js error?

I'm using the uncaughtException event of process to detect errors. The provided error object does not have the code property, but the Node.js documentation says that code is a property of the Error class. Is the error object provided by uncaughtException different from the Error class?
It depends on the error which is caught, i.e. not all errors actually have the code property set.
Consider the following two simple examples:
// example 1
const express = require('express')
const app = express();
const port = 3000;
process.on('uncaughtException', (error) => {
console.log(error)
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
If something else is already listening on the 3000 port, the following error-info will be printed to the console:
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1298:14)
at listenInCluster (net.js:1346:12)
at Server.listen (net.js:1434:7)
...
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10) {
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 3000
}
As you can see the system error EADDRINUSE has a code property set.
On the other hand this
// example 2
process.on('uncaughtException', (error) => {
console.log(error)
});
someFunc();
will print:
ReferenceError: someFunc is not defined
...
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at internal/main/run_main_module.js:17:11
As you can see ReferenceError does not have the code-property set.

How do I fix this error? Mongooseconnection error

I am trying to run my application and it keeps giving me this error and this is from a demo app from sitepoint. This is the details of the app
https://www.sitepoint.com/build-simple-beginner-app-node-bootstrap-mongodb/
The error:
throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
^
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mo
ngoose.connect()` or `mongoose.createConnection()` is a string.
at new MongooseError (C:\Users\tychi\WebstormProjects\demo-node-app\node_modules\mongoose\lib\error\mongooseError.js:
10:11)
at NativeConnection.Connection.openUri (C:\Users\tychi\WebstormProjects\demo-node-app\node_modules\mongoose\lib\conne
ction.js:579:11)
at Mongoose.connect (C:\Users\tychi\WebstormProjects\demo-node-app\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:\Users\tychi\WebstormProjects\demo-node-app\start.js:4:10)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at startup (internal/bootstrap/node.js:285:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
[nodemon] app crashed - waiting for file changes before starting...
the code:
require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect(process.env.demoapp ,
{ useNewUrlParser: true },
{ useUnifiedTopology: true })
mongoose.connection
.on('open', () => {
console.log('Mongoose connection open');
})
.on('error', (err) => {
console.log(`Connection error: ${err.message}`);
});
require('./models/Registration');
const app = require('./app');
const server = app.listen(3000, () => {
console.log(`Express is running on port ${server.address().port}`);
});

mongoose.connect not invoking callback after connection to mongodb

I'm trying to make a connection to MongoDB using Mongoose. But it does neither throw a error nor connect to the database. Following is my code.
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
const mongoose = require('mongoose');
console.log('Hi, there!!');
mongoose.connect('mongodb://localhost:27017/db_name', (err) => {
console.log('Callback called');
if(err) throw err;
console.log('Connected to database');
})
In the above code none of the console.log inside the callback do happen. But any place outside the mongoose.connect do work like console.log('Hi, there!!')
Versions Used
express: 4.0.0
mongoose: 3.8.40
node: 7.7.3
mongodb: 3.4.0
Using mongoose: 3.8.40 I got this in the console :
{ Error: Cannot find module '../build/Release/bson'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/kevin/nemeacreation/sites/test/stackoverflow/node_modules/bson/ext/index.js:15:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3) code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
Hi, there!!
upgrading to "mongoose": "~4.4" fixed it for me. I got the answer here : https://stackoverflow.com/a/35516644/2829540
For info the latest release of mongoose is 4.10.4

an error occures when i try to connect to my mongo database

I'm new in using full stack to build an application, i tried to create a connection that allow to the client side to connect to my mongo database. here my server.js code file
console.log("Server running...!");
var mongo=require('mongodb').MongoClien,
client=require('socket.io').listen(8080).sockets;
mongo.connect('localhost:27017/chat',function(err,db){
if(err) throw err;
client.on('connection',function(socket){
console.log('someone has connected !');
//waiting for input
socket.on('input',function(data){
console.log(data);
});
});
});
when i run this with node.js i get the following error in my command prompt window.
Server running...!
C:\Users\azus\Desktop\Psirt\code-master\server.js:6
mongo.connect('mongodb://127.0.0.1:27017/chat',function(err,db){
^
TypeError: Cannot read property 'connect' of undefined
at Object.<anonymous> (C:\Users\azus\Desktop\Psirt\code-master\server.js:6:7
)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)
at node.js:999:3
I checked my mongo server and it's running !!
I am working with the latest up to date version, i'm doing this on windows.
'MongoClien' is unknown
You are trying to end a line with a comma
Change
var mongo=require('mongodb').MongoClien,
to:
var mongo = require('mongodb').MongoClient;

fs library : Error when use it

I use fs library for reading file from system. I don't know I use this library and meet error.
I'm using PhpStorm. at line : fs.readFile() : there's a line under that, that notice me : unresolved function or method readFile(). It means IDE doesn't figure out where is this function. Nevertheless, I have checked fs.js and I see no problem.
I receive this error when running:
events.js:72
throw er; // Unhandled 'error' event
^ Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at Server.listen (net.js:1127:5)
at Object. (/home/hqt/PhpstormProjects/NodeApp/sample/AsynchronouslyReading.js:21:4)
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)
Here is my code :
var http = require('http');
var fs = require('fs');
// create http server
http.createServer(function(req, res) {
fs.readFile('helloworld.js', 'utf-8', function(err, data) {
res.writeHead(200, {'content-type' : 'text/plain'});
if (err) res.write('could not find or open file');
else res.write(data);
// ending
res.end();
});
}).listen(8124, function() {console.log('server is running at 8124');});
console.log('server is runnint at 8124 port');
Please help me figure out why. I'm using Ubuntu machine for development.
Thanks :)
That's because something else is already listening on port 8124. On Linux, you can use something like netstat -tanp | grep LISTEN | grep 8124 to see what.

Resources