I want to add a script in app.js file here is the code:
app.helpers({
renderScriptsTags: function (all) {
if (all != undefined) {
return all.map(function(script) {
return '<script src="/javascript/' + script + '"></script>';
}).join('\n ');
}
else {
return '';
}
}
});
app.dynamicHelpers({
scripts: function(req, res) {
return ['canvasjs.min.js'];
}
});
while executing its give me an error shown below:
app.helpers({
^
TypeError: Object function (req, res, next) {
app.handle(req, res, next);
} has no method 'helpers'
at Object.<anonymous> (/var/www/html/nodeproject/helloworld/index.js:27:5)
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
As your error is showing app does not have helper funtion. Memeber functions of app are listed here http://expressjs.com/en/api.html
if you want to use helpers you will have to create them explicity like
//helper.js
module.export = function(){
// Your logic here
};
//usage
var helper = require('./relative/path/to/helper');
var getValueFromHelper = helper();
hope it helps :)
Related
Hello I'm having a silly problem but I can't find the solution. When I try start my localhost I get an error saying this :
Error: Cannot find module 'passport'
Require stack:
- D:\Other\Projects\Code\Powershell\shopping-cart\config\passport.js
- D:\Other\Projects\Code\Powershell\shopping-cart\routes\index.js
- D:\Other\Projects\Code\Powershell\shopping-cart\app.js
- D:\Other\Projects\Code\Powershell\shopping-cart\bin\www
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
at Function.Module._load (internal/modules/cjs/loader.js:864:27)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (D:\Other\Projects\Code\Powershell\shopping-cart\config\passport.js:1:16)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (D:\Other\Projects\Code\Powershell\shopping-cart\routes\index.js:5:16)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\config\\passport.js',
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\routes\\index.js',
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\app.js',
'D:\\Other\\Projects\\Code\\Powershell\\shopping-cart\\bin\\www'
}
I understand that it is not finding the passport.js file, I have checked the path name more than 10 times and I saw another problem where the guy had not installed the passport module, so I did that as well but it is still throwing the same error. This is what my index.js looks like :
var express = require('express');
var router = express.Router();
var Product = require('D:/Other/Projects/Code/Powershell/shopping-cart/models/product');
var csrf = require('csurf');
var passport = require('D:/Other/Projects/Code/Powershell/shopping-cart/config/passport.js');
var csrfProtection = csrf();
router.use(csrfProtection);
/* GET home page. */
router.get('/', function(req, res, next) {
Product.find(function(err, docs) {
var productChunks = [];
var chunkSize = 3;
for (var i = 0; i < docs.length; i += chunkSize) {
productChunks.push(docs.slice(i, i + chunkSize));
}
res.render('shop/index', { title: 'Shopping Cart', products: productChunks });
});
});
router.get('/user/signup', function (req, res, next) {
res.render('user/signup', {csrfToken: req.csrfToken()});
});
router.post('/user/signup', passport.authenticate('local.signup', {
successRedirect: '/user/profile',
failureRedirect: '/user/signup',
failureFlash: true
}));
router.get('/user/profile', function(req, res, next) {
res.render('user/profile');
});
module.exports = router;
Any help would be appreciated, thanks! Also , I have another problem , can I post another question in like one hour? (is it allowed to have more than 1 questions posted?)
Im trying to create auto increment field in mongoose but i cant create for some reason
This is my app.js
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var Artist = require('./models/artist');
mongoose.connect('mongodb://localhost/auto-increment');
app.get('/', function(req, res){
Artist.create({
name: "artistName",
fpimage: "Cover"
}, function(err, artist){
if(err) {
console.log(err);
} else {
artist.save();
console.log(artist);
res.send('Hi ')
}
});
});
// Set Port
app.set('port', (process.env.PORT || 3000));
// Run Server
app.listen(app.get('port'), function(){
console.log('Server has started on Port: '+app.get('port'));
});
and this is my artist model
var mongoose = require("mongoose");
var autoIncrement = require('mongoose-auto-increment');
var artistSchema = new mongoose.Schema({
name: String,
fpimage: String
});
artistSchema.plugin(autoIncrement, 'Artist');
module.exports = mongoose.model('Artist', artistSchema);
When i run the app, i get following the error message
fn(this, opts);
^
TypeError: fn is not a function
at Schema.plugin (C:\Users\tjesu\Desktop\pidal\auto-increment\node_modules\mongoose\lib\schema.js:1060:3)
at Object.<anonymous> (C:\Users\tjesu\Desktop\pidal\auto-increment\models\artist.js:10:14)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\tjesu\Desktop\pidal\auto-increment\app.js:4:14)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
How can i fix this error? What should i do?
From the docs, it looks like you're missing two things:
You need to initialize autoIncrement:
autoIncrement.initialize(mongoose.connection);
You need to change your call to:
artistSchema.plugin(autoIncrement.plugin, 'Artist');
use autoIncrement.plugin instead of autoIncrement in your model
like:
artistSchema.plugin(autoIncrement.plugin, 'Artist');
and need initialize autoIncrement once
var connection = mongoose.createConnection("mongodb://localhost/auto-increment");
autoIncrement.initialize(connection);
and can use get like
app.get('/', function(req, res){
var artist = new Artist({name: "Telmuun",fpimage: "Cover"});
artist.save(function(err, saved) {
if(err) {
console.log(err);
}
else {
res.send('saved');
}
});
});
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.
I'm working on a new project and I'm new with baucis for node.js. This is the code:
var name = 'order'
, baucis = require('baucis')
, controller = baucis.rest(name)
, paths = config.paths;
controller.request(function (req, res, next) {
if (req.isAuthenticated())
return next();
return res.send(401);
});
controller.documents(function (req, res, next) {
if (typeof req.baucis.documents === 'number') return next();
[...]
next();
});
And this is the error:
/Users/fil/[...]/order.js:31
controller.documents(function (req, res, next) {
^
TypeError: Object function router(req, res, next) {
router.handle(req, res, next);
} has no method 'documents'
at Object.<anonymous> (/Users/fil/[...]/order.js:31:12)
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at /Users/fil/[...]/app.js:29:5
at Array.forEach (native)
at Object.<anonymous> (/Users/fil/[...]/app.js:27:57)
Any idea of the reason why this happends?
Hard to say 100% for sure, but I am guessing you are trying to use the controller.documents method with a new version of baucis, one that does not support this middleware stage.
If this is so, read about using outgoing stream transforms in the README.md file.
HTH
I am trying to run a node.js getting this error
events.js:130
throw TypeError('listener must be a function');
My code is,
var connect = require("connect");
var http = require("http");
var app = connect();
// Logging middleware
app.use(function(request, response, next) {
console.log("In comes a " + request.method + " to " + request.url);
next();
});
// Send "hello world"
app.use(function(request, response) {
response.writeHead(200, { "Content-Type": "text/plain" });
response.end("Hello world!\n");
});
http.createServer(app).listen(1337);
Now execute this code on terminal as node app.js I get an error
events.js:130
throw TypeError('listener must be a function');
^
TypeError: listener must be a function
at TypeError (<anonymous>)
at Server.EventEmitter.addListener (events.js:130:11)
at new Server (http.js:1850:10)
at Object.exports.createServer (http.js:1880:10)
at Object.<anonymous> (/var/www/express/app.js:17: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)
I installed node.js and express.js. How to execute this code ?
I found the mistake.
The problem is that the program(app.js) is not in the root of framework folder. I changed the location of this program to the express frameworks root folder. Now it works perfectly.