Cannot read property '_header' of undefined - javascript

The offending code is the app.use(express.static("web")) line.
var express = require('express')();
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//app.get('/', function(res, req) {
// res.sendFile(__dirname + '/www/index.html');
//})
app.use(express.static("web"));
which returns the following error in console:
/Users/matthewwalker/CVLGBT/CVLGBT/www/node_modules/finalhandler/index.js:92
if (!err && res._header) {
^
TypeError: Cannot read property '_header' of undefined
at /Users/matthewwalker/CVLGBT/CVLGBT/www/node_modules/finalhandler/index.js:92:21
at Function.handle (/Users/matthewwalker/CVLGBT/CVLGBT/www/node_modules/express/lib/application.js:170:5)
at app (/Users/matthewwalker/CVLGBT/CVLGBT/www/node_modules/express/lib/express.js:38:9)
at Object.<anonymous> (/Users/matthewwalker/CVLGBT/CVLGBT/www/index.js:2:11)
at Module._compile (internal/modules/cjs/loader.js:689:30)
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)
Other times when I change up the code slightly I get express is not defined. File structure: index.js and node modules under root, while all html/css/js/imgs are under /web. I'm not sure why this header flag is being thrown. When I use the app.get line, it only sends the html file but no images or js. I need to be able to serve the whole directory of /web.

You're constructing an app instance on the first line, and then calling it on line 2:
var express = require('express')();
var app = express();
You should do this instead:
var express = require('express');
var app = express();
Or this:
var app = require('express')();
You can find more information about the express api here.

Related

TypeError: app.use() requires a middleware function

Every time I try to run the app after add users routes it gives error something like this
C:\Users\adity\Desktop\thinkster\medium-api\node_modules\express\lib\application.js:210
throw new TypeError('app.use() requires a middleware function')
^
TypeError: app.use() requires a middleware function
at Function.use (C:\Users\adity\Desktop\thinkster\medium-api\node_modules\express\lib\application.js:210:11)
at Object.<anonymous> (C:\Users\adity\Desktop\thinkster\medium-api\app.js:15:5)
at Module._compile (internal/modules/cjs/loader.js:689:30)
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)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
[nodemon] app crashed - waiting for file changes before starting...
This is my file Structure
app.js
const app = express();
app.use('./routes');
routes/index.js
const express = require('express');
const router = express.Router();
router.use('/api', require('./api'));
module.exports = router;
routes/api/index.js
const express = require('express');
const router = express.Router();
router.use('/', require('./users'));
module.exports = router;
routes/api/users.js
const express = require('express');
const router = express.Router();
router.get('/users', (req, res) => res.send('Hello world'));
module.exports = router;
I found a website called realworld.io and I am following there a way of making nodejs API and here I get stuck every time. And I can't find where the problem is.
The code app.use('./routes'); in your app.js is incorrect (there is only one string parameter, which violates the app.use() syntax).
To define "routes" correctly, the code would look like:
// app.js
const app = express();
const routes = require('./routes');
app.use('/', routes);

Nodejs - throw unpredictable error as `property 'get' of undefined`

I am trying to run my node app. But I am getting an error, which I am not able to understand. please any one help me to understand this?
here is my code :
var express = require("express"),
app = express(),
path = require("path");
app.get("/", function( req, res ) {
res.sendfile( path.join(__dirname + '/index.html'));
});
var adminRouter = express.Router();
adminRouter.get('/', function(req, res) {
res.send('I am the dashboard!');
});
app.use("/admin", adminRouter);
app.listen(process.env.PORT, process.env.IP);
console.log("basic app listeners!");
the error I am getting is :
adminRouter.get('/', function(req, res) {
^
TypeError: Cannot read property 'get' of undefined
at Object.<anonymous> (/home/ubuntu/workspace/server.js:16:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:990:3
Can any one help me? I am running my app in cloud9.
thanks in advance!!
Your express version less than +4 , probably version 3. Try
npm uninstall express --save
Then re-install.
adminRouter.get('/', function(req, res) {
res.send('I am the dashboard!');
});
try it like this as well.
adminRouter.route('/').get(function(req,res){
res.json({'hello there from main route!'});
});
Actually this is express version problem.
express.Router(); is supported on version 4.x and cloud 9 support by default 3.x
change your package.json
"express": "^4.15.2",
and delete node_module folder
then run
npm install

Calling helper function in node.js within callback?

I'm fairly new to programming with node.js and am not quite sure why I am getting this error. The function looks to be set up correctly, and I don't believe I have any asynchronous problems b/c those should be account with the self variable I put in place (I think). I did try w/o that too, using simple var consolePrint(...) Anyways, this is my code below and the error log below that.
/* global __dirname */
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var self = this;
//CALLING HELPER FUNCTION HERE
var server = app.listen(8000, self.consolePrint(server));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/public', express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/views/index.html');
});
//---------------helper function(s)-------------------//
self.consolePrint = function(serverVar){
var host = serverVar.address().address;
var port = serverVar.address().port;
console.log('Example app listening at http://%s:%s', host, port);
}
and error:
C:\Users\Daniel\Desktop\workspace\alarm_clock\index.js:17
var server = app.listen(8000, self.consolePrint(server));
^
TypeError: undefined is not a function
at Object.<anonymous> (C:\Users\Daniel\Desktop\workspace\alarm_clock\index.js:17:36)
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
12 May 01:01:36 - [nodemon] app crashed - waiting for file changes before starting...
This will resolve the problem:
var server = app.listen(8000, function(){self.consolePrint(server)});
You are using the function before defining it. Put the listen function below the 'self.consolePrint' assignment statement or assign the before using it, it will work.

node.js + express error: Cannot read property 'handle' of undefined

I am new to node.js. I was trying a script which is using express module.
I have installed express, using,
npn install express
When I run the code I got the error
TypeError: Cannot read property 'handle' of undefined
at Function.app.use (c:\node_modules\express\lib\application.js:113:9)
at Object.<anonymous> (c:\node\uploadResize.js:13:6)
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
How to solve this issue?
Here is my node script.
var http = require('http'), // Libraries
util = require('util'),
fs = require('fs'),
couch = require('cradle'),
connect = require('express'),
endsWith, // Internal Functions
determineMimeType,
upload;
connect()
.use(connect.bodyParser())
.use(upload)
.listen(3000);
upload = function (req, res, next) {
// function body
}
You need to assign upload a value before passing it to app.use
Use something like this:
var app = express();
app.configure(function() {
var hourMs = 1000*60*60;
app.use(express.static('c:\\node', { maxAge: hourMs }));
app.use(express.directory('c:\\node'));
app.use(express.errorHandler());
});
the code not is npm install express ??
then the dir node_modules in the same dir in witch you have your app
C:\node\node_modules
C:\node\app.js

Trying to setup Node.js (Express) to work with vhosts, and getting unexpected errors

I'm trying to set it up to work with a couple vhosts, so that I could manage everything through the one node app; but I've been getting this error.
It's late right now, so my mind isn't 100%, but hopefully someone can see something I don't.
/vhosts/app.js:13
.listen(3000);
^
SyntaxError: Unexpected token ;
at Module._compile (module.js:437:25)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Here's the code:
var express = require('express');
var app = express();
app
.use(express.vhost('localhost', require('/first/vhost/app.js').app)
.use(express.vhost('localhost2', require('/second/vhost/app.js').app)
.listen(3000);
And that first vhost app runs fine, if I got and run it manually with node app.
As Brett points out you are missing the last bracket:
var express = require('express');
var app = express();
app
.use(express.vhost('localhost', require('/first/vhost/app.js').app))
.use(express.vhost('localhost2', require('/second/vhost/app.js').app))
.listen(3000);
You should not use require inside the the Connect middleware. This way it would also have been easier to spot :-)
var express = require('express');
var app = express();
var first = require('/first/vhost/app.js').app;
var second = require('/second/vhost/app.js').app;
app
.use(express.vhost('localhost', first))
.use(express.vhost('localhost2', second))
.listen(3000);

Categories

Resources