node.js express.js object is not a function call_non_function - node.js

I have this error:
TypeError: object is not a function
at Object.CALL_NON_FUNCTION (native)
For this line:
var app=express();
I tryed to install express/connect again, but.. nothing.
Thanks!
EDIT
I'm express 2.5.8.
my code:
error:
var http=require('http');
var app=express();
var server=http.createServer(app);
(i forget why i need to use this code, i think for cookie handshake works.
I have resole the probleme (hanskake cookie) editing manager.js, so i dont need to use this code. But can be interesting to understand why no works (and why i wanted to use)).
no error:
var app=express.createServer();

You have the wrong express version. You can only create the server with express() in v3.x.x. Before this version, express can not be called as a Function. Try either changing your code to create the app the old way or try updating express.

Related

Parameter restriction with Node.js

What I want to do is check the script URL for a parameter & display the content of that parameter like:
www.mywebsite.com/mynodescript.js?parameter=i+am+new+to+node!
Now I want to display "I am new to node!" on browser screen and if the parameter is not present I just want to exit.
edit:
I found this code but I am not sure how to deploy it
var url = require('url');
var url_parts = url.parse(request.url, true);
var query = url_parts.query;
Note: i want to upload my script on heroku & want it to call it remotely
When you say you don't know how to deploy it, I'm assuming you don't have a http server setup yet?
Look at using Express (http://expressjs.com/). It's easy enough to get started with.
Create a file called app.js like this:
const express = require('express')
const app = express()
// This handles the path /mynodescript.js You can create a bunch of functions like this to handle different paths. See the express docs for more.
app.get('/mynodescript.js', (req, res)=>{
let parameter = req.query.parameter; // <-- Could also do let {parameter} = req.query This is where you would pull out your url parameters
if(parameter){
res.send(parameter); // <-- this sends it back to the browser.
}else{
res.status(422).end(); // <-- you can set a status here or send an error message or something useful.
}
})
app.listen(3000, () => console.log('Example app listening on port 3000!'))
Start the script using node app.js from the same directory.
Then open a browser and go to http://localhost:3000/mynodescript.js?parameter=i+am+new+to+node and you should see your parameter
Note that you will have to install express first npm install express --save
Note that you do not have to use express. There are quite a few http libraries available for nodejs. Or you can use the built-in http server (https://nodejs.org/api/http.html). It's good to get familiar with the NodeJS docs, but their http server is cumbersome to work with.

why is my express API not responding?

Background
I am testing a simple Hello World app using NodeJs v7 and express in cloud9.
I am trying to make my example work but I am failing.
Problem
All my cloud9 configurations are fine so that is not the problem. The problem is my app. When i debug, the route "api/v1/HolaBananas" never gets called and I don't know why!
Even worst, when i make the request, the browser just hangs, like it is waiting for an answer from the server that will never come!
Code
The index.js has the initialization and launch code. It is important for me that I keep this separate from api.js for modular reasons.
index.js
"use strict";
const express = require("express");
const app = express();
app.use("/api/v1", require("./api.js"));
app.listen(process.env.PORT);
console.log(`Server listening on port ${process.env.PORT}!`);
The api.js simply contains the routes and what they are supposed to do.
api.js
"use strict";
const express = require("express");
module.exports = function() {
const api = express.Router();
api.get("/HolaBananas", function(req, res){
res.send("hello bananas!");
});
return api;
};
Question
I am sure I am not using api.get in the right way, but I truly want to separate my initialization and launch code from the api.
How can I fix my code so it works?
Note
I am following the course
https://www.edx.org/course/introduction-mongodb-using-mean-stack-mongodbx-m101x-0
You can fix it by two following ways
var api = require("./api.js")();
app.use("/api/v1", require("./api.js"));
as API.js return a function reference. so you need to call that function to access route.
Or You need to modify your api.js as follows if you don't want to change index.js
"use strict";
const express = require("express");
const api = express.Router();
api.get("/HolaBananas", function(req, res){
res.send("hello bananas!");
});
module.exports = api;
Solution
While Vikash Sharma's solution would work, I figured it out by following the recommendation of Jayant Patil and to read this other question:
https://codereview.stackexchange.com/questions/51614/exporting-routes-in-node-js-express-4
Turns out we had the same issue, but the given answer to that question also allows me to encapsulate the api file completely inside a function, preserving its scope.
Still, kudos++ for trying!
There is one subtle thing about it: You have to invoke the function that you export in your api.js and use the router object that is returned from that function, so here is what you should do:
You have to replace this line:
app.use("/api/v1", require("./api.js"));
with this:
app.use("/api/v1", require("./api.js")());

Validate my understanding on Express Routes

Actually, before I get into the question, when I do anything like
const app = express()
app is an instance of the entire express module right? Meaning, when I do app.route, route is it an Express method right or a NodeJS method, since Node has .route as well? Anyways... I just wanted to double check this.
app.route('/games')
.post(postGame)
.get(getGames);
app.route('/games/:id');
.get(getGame)
.delete(deleteGame);
Is this the same as... and if not... why would one choose one over the other?
app.get('/games');
app.post('/games');
app.get('/games/:id');
app.delete('games/:id');
Sorry, it's just been a while since I have used Express, and couldn't find anything about this specific problem. Thanks!
app is an instance of the entire express module right?
Yes, the app object is create by calling the top-level express() function exported by the Express module. That set the default http headers, render options.... and wrap the http node module:
app.listen = function listen() {
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
You can see more in the source code here (really readble)
route is it an Express method right or a NodeJS method
Route is an express object and nodeJs don't provide a routing system like express.
And for your example yes it's totally the same. They stores the handlers function in the same this._router.route

Can't see console.log in routes.js using node webkit and express

I am developing a desktop app using node webkit and express. I use sublime build system to debug. I can see client pages log. But in routes I can't see any logs or alert message in sublime console, cmd console and nodewebkit devtool.
app.post('/', function(req, res) {
console.log('Hello');
// my code
});
I am sure it's hit since my page is rendered. Sorry I am new to node-webkit. Thanks for help
It sounds like your routes file isn't being hit. Try including the following in your app.js:
var router = express.Router();
var routes = require('./app/routes.js'); //the path to your routes file
router.use('/',routes);
Also in your routes file you will need a few requires:
var express = require('express'),
app = express().Router();
As far as debugging, I use node-inspector, you can install it via npm install node-inspector. They have instructions on their page of how to use it, but it's basically similar to a Chrome inspector console, you should set a breakpoint on your route function, to see if it's being triggered.
This is documented on the Express page under the express.Router() section at the bottom of the page, you will need to include the Router function in your call to break out your routes into a different file
If that doesn't work, you can post more of your code and we can try to help out.

Express 4.x with Typescript

I am trying to make a express app with typescript.
This is my code so far:
//<reference path="./server/types/node.d.ts"/>
//<reference path="./server/types/express.d.ts"/>
import express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('hi');
});
app.listen(3000);
nothing really shocking, I am just trying to make this work, but somehow, always when I try translate this file to a js file. I get strange errors, even if I change the express version to 3.1 (the express.d.ts is just supported for express 3.1 not for 4.x)
Any idea, where I can get a express.d.ts file for express 4.x or what I am doing wrong?
>> error TS2071: Unable to resolve external module ''express''
>> error TS2071: Module cannot be aliased to a non-module type.
>> error TS2095: Could not find symbol 'express'.
You reference comments are wrong. There needs to be three slashes /// :
///<reference path="./server/types/node.d.ts"/>
///<reference path="./server/types/express.d.ts"/>
The only way you can get that error if you are using this reference file https://github.com/borisyankov/DefinitelyTyped/blob/master/express/express.d.ts#L26 was that your reference comments were wrong and typescript was not reading that express.d.ts :)
Might I add that because you are using express 4.x and the type definitions are not up-to-date, you will be missing some key features that are central to express 4.x, such as Routers.
You have two choices at this point: update the type definitions, or use
var express = require('express');
instead, which removes some of the benefits that typescript provides, but is something you're bound to run into in the future with other node modules, such as mongoose.

Resources