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
Related
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);
from app.js
// REQUIRE ROUTES
var commentRoutes = require("./routes/comments"),
bpostRoutes = require("./routes/bposts"),
indexRoutes = require("./routes/index");
//USE ROUTES
app.use("/", indexRoutes);
app.use("/bposts", bpostRoutes);
app.use("/bposts/:id/comments", commentRoutes);
from routes/index.js
var express = require("express");
var router = express.Router();
//LANDING -root route
router.get("/", function(req, res){
res.render("landing");
});
module.exports = router;
I am currently trying to setup all my routes before I create a DB or do anything meaningful for the blog. When I encountered this problem the first time it was because I didn't use
module.exports = router;
on each route page I had. Express router is installed and saved to package.json. Each time I have run into this error it's a quick fix because I didn't include export statement. Now I have finally remembered to add it in and I am still receiving this error message. Any suggestions or advice would be greatly appreciated!
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a Object
at Function.use (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:458:13)
at EventEmitter.<anonymous> (/home/ubuntu/workspace/node_modules/express/lib/application.js:220:21)
at Array.forEach (native)
at EventEmitter.use (/home/ubuntu/workspace/node_modules/express/lib/application.js:217:7)
at Object.<anonymous> (/home/ubuntu/workspace/app.js:25:5)
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)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3
I use an IDE callled C9(Cloud 9) and only use the free service, which means lower performance and potential downtime prioritizing user space for paid cx. So I logged out of my account and relogged in. It then cleared and cleaned up the workspace as it was launching my app. After this I was able to preview my app without any issue. Time to upgrade! I changed nothing in the code, no additions or subtractions. It's the first time I have seen it happen in 6 months I have used it. This did resolve the issue. Thanks for all the help and suggestions!
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
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);
clean install node ,express, socket.io on linux environment using npm. I try to runsocket.io sample from socket.io official source, I am getting error socket.io-client module not found on command line.
I work around few hours to solve this. but I didnt found any soutions.
Error :
module.js:337
throw new Error("Cannot find module '" + request + "'");
^
Error: Cannot find module 'socket.io-client'
at Function._resolveFilename (module.js:337:11)
at Function._load (module.js:279:25)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at Object.<anonymous> (/home/rajuk/Documents/nodeSamples/node_modules/socket.io/lib/socket.io.js:12:14)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:32)
at Function._load (module.js:311:12)
at Module.require (module.js:359:17)
socket.io install result
socket.io#0.9.14 /usr/local/lib/node_modules/socket.io
├── base64id#0.1.0
├── policyfile#0.0.4
├── redis#0.7.3
└── socket.io-client#0.9.11
here is my code(app.js)
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(80);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
and node_modules directory contains socket.io and express packages.
I am trying to run server like this
$ node app.js
how to resolve it?
Clone this git depositery : https://github.com/yrezgui/socket.io-simple-demo
And tell me if you still have an error.