Error Blog
Hi Guys ,
This blog is about the error TypeError: app.configure is not a function
Problem Statement
unable to start node js express application
Error Details
error Log
ERROR DETAILS
app.configure(function(){
^
TypeError: app.configure is not a function
at Object.<anonymous> (F:\\study\gitRepo\new\learn\nodejs\expressjs\app.js:5:5)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Error Code
You will face this error if you are using app.configure in an express js application.
var express = require('express'),
app = express(),
port = parseInt(process.env.PORT, 10) || 8080;
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
});
app.listen(port);
app.post("/myRoute", function(req, res) {
console.log(req.body);
res.send({ status: 'SUCCESS' });
});
Solution
app.configure is removed in Express 4 and above .
Code Changes
add the following import in your component.ts file .
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.post('/api/users', function(req, res) {
console.log(req.body);
res.send('success');
});
app.listen(8082);
OUTPUT
works
Thanks for reading . Let me know your thoughts in the comments below .