node express module throwing 'has no method urlencoded' error - node.js

var express=require('express')
var app=express();
console.log("Encoded ",express.urlencoded());
app.use(express.urlencoded());
The above code throws the following error :
[user#localhost nodejs]$ node program.js
/home/user/Desktop/nodejs/program.js:41
console.log("Encoded ",express.urlencoded());
^
TypeError: Object function createApplication() {
var app = function(req, res, next) {
app.handle(req, res, next);
};
mixin(app, proto);
mixin(app, EventEmitter.prototype);
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.init();
return app;
} has no method 'urlencoded'
at Object.<anonymous> (/home/user/Desktop/nodejs/program.js:41:32)
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:902:3
Seems like a similar question here - express.js trouble with connect modules but I'm already having express3.0.0 checked using the suggestion listed here - Find the version of an installed npm package
I also read the Api docs here - http://expressjs.com/api.html and they list urlencoded()
Please help.
I would also like to point, that I also tried using bodyParser() but that too gave the same error of has no method

The express guide is a bit-outdated.
For others having the same problem, the solution is that those methods have moved to a new module body-parser
Sample Code
var express=require('express');
var app=express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());

Related

A regex works fine with Express Route Tester but it failed when it used with NodeJS

I used express in a NodeJs project, and i want to be able to request my server with theses routes :
/dogs
/pinguin
/bear
/wolf
/cat
/rat
I use a regex for this (http://forbeslindesay.github.io/express-route-tester/) :
Express Route Tester
It works correctly with express route tester but it failed when i tried with NodeJS
My code :
var express = require('express');
var app = express()
app.get("\/(dogs|pinguin|bear|wolf|cat|rat)", function (req, res) {
res.send('dogs or pinguin');
});
error :
return new RegExp(path, flags);
^
SyntaxError: Invalid regular expression: /^\/(?(?:([^\/]+?))|pinguin|bear|wolf|cat|rat)\/?$/: Invalid group
at new RegExp (<anonymous>)
at pathtoRegexp (C:\Users\Corentin\node_modules\path-to-regexp\index.js:128:10)
at new Layer (C:\Users\Corentin\node_modules\express\lib\router\layer.js:45:17)
at Function.route (C:\Users\Corentin\node_modules\express\lib\router\index.js:500:15)
at Function.app.<computed> [as get] (C:\Users\Corentin\node_modules\express\lib\application.js:481:30)
at Object.<anonymous> (C:\Users\Corentin\Documents\mesProjets\mdm\mdm-api\routes.js:36:5)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
try with the below code
app.get(/^\/(dogs|pinguin|bear|wolf|cat|rat)/, function (req, res) {
res.send('dogs or pinguin');
});

Async/Await not running in Azure App Service

Within my Azure App Service Node.js backend I cannot seem to get the Javascript async/await feature to run. I have changed the default version of Node.js within application settings and package.json to above 7.6. (Changed to 8.9.0)
I would like to use this feature within a custom Express router shown here:
var express = require('express'),
bodyParser = require('body-parser');
var router = express.Router();
router.get('/', function (req, res, next) {
res.status(200).send('GET: This is a test response!');
});
router.post('/:id', async function (req, res, next) {
var context = req.azureMobile;
var newLovedOne = req.body.lovedone;
var newTie = req.body.tie;
console.log('POST: newLovedOne ', newLovedOne);
console.log('POST: newTie ', newTie);
try {
await context.tables('Tie').insert(newTie);
await context.tables('LovedOne').insert(newLovedOne);
} catch (error) {
res.status(500).send('Insert failed!');
}
});
module.exports = router;
Attempting to start the app with the above router produces this:
Application has thrown an uncaught exception and is terminated:
SyntaxError: missing ) after argument list
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
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 Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (D:\home\site\wwwroot\app.js:12:20)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
To verify whether the Node.js version is correctly set, you can go to your root and open the iisnode.yml file. Make sure it has the following line with the correct version:
nodeProcessCommandLine: "D:\Program Files (x86)\nodejs\8.9.0\node.exe"

TypeError: require(...) is not a function (Node.js) [server.js] [duplicate]

This question already has answers here:
NodeJs : TypeError: require(...) is not a function
(8 answers)
Closed 6 years ago.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true,
}));
app.use('/', require('../routes')(express));
exports.server = app.listen(port, () => {
console.log('Server active on', port);
});
Am I missing a npm package?
Error
TypeError: require(...) is not a function
at Object.<anonymous> (C:\xampp\htdocs\url-shortener\src\server.js:12:34
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)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:418:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:533:3
Your ../routes source file is not exporting a function.
Try replacing this line:
app.use('/', require('../routes')(express));
with:
console.log( require('../routes') );
I bet it prints an object ({ ... }) rather than a function.
Inside your ../routes file you need to explicitly export the important function:
module.exports = importantFunction;
function importantFunction(string) {
console.log(string);
}
Now you scripts which import it can call it.
require('../routes')("hi") // should log "hi"

Object function createServer() has no method 'bodyparser'

I am new to nodeJS. I am trying to use different middlewares with connect middleware.
this is my code:
var connect = require('connect');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var app = connect()
.use(connect.bodyParser())
.use(connect.cookieParser('tobi is a cool ferret'))
.use(function(req, res){
console.log(req.cookies);
console.log(req.signedCookies);
res.end('hello\n');
}).listen(3000);
I have installed every middleware through npm.
I am getting this error while running this file.
/home/dipesh/Desktop/temp/temp.js:5
.use(connect.bodyParser())
^
TypeError: Object function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
} has no method 'bodyParser'
at Object.<anonymous> (/home/dipesh/Desktop/temp/temp.js:5:14)
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:929:3
Any suggestions?
.use(bodyParser())
not
.use(connect.bodyParser())
You have required body-parser, but then never used it.
You are essentially doing
var a = function(){};
var b = {};
b.a();
which is not correct because b has not 'a' property.

Node.js error no server startup

As a part of angular.js course, i downloaded and installed node.js, inserted server.js file in main folder with following content :
var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);
and then tried to run server by cli, but im getting error in cli:
TypeError: Object function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
} has no method 'static'
at Object.<anonymous> (C:\Program Files (x86)\nodejs\server.js:4:19)
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:906:3
try:
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic('angularjs'));
app.listen(5000);
Should work for you!
EDIT:
With Connect 3.0 .static() is moved to a separate package called serve-static.
So you'll have to install that before running this code.
Edit: THis is for an older version of Node and therefore doesn't answer the question. See the comments below.
To use connect, you need to setup the environment, and then create the server
var connect = require('connect');
car app = connect();
app.static("../angularjs");
connect.createServer(app).listen(5000);
You may also be able to do it the brief way that you have by:
connect.createServer(
connect().static("../angularjs")
).listen(5000);

Resources