How to use exec-php(node js) in MVC framework like zend? - node.js

Basically what I want to do over here is to get data into node server js, data which is returned from a function which is written into a Zend model.
My node js server code is as below:
var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var execPhp = require('exec-php');
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
execPhp('../application/modules/front/models/Dbtable/Postcontent.php',
function(error, php, outprint){
php.fetchEntryAll(function(error, result){
io.sockets.emit('showfeeds', result);
});
});
server.listen( 7000 );
so when I tried to execute above code it gives me error like below:
Error: Command failed: PHP Fatal error: Class 'Zend_Db_Table_Abstract' not found in /Data/www/XXX/application/modules/front/models/Dbtable/Postcontent.php on line 3
same code works when I use core PHP instead of Zend.
So I believe that we can't use OOP stuff with exec-php.
Is there any solution here?
Reference link to exec-php:
https://www.npmjs.com/package/exec-php

The problem you ran into is not about OOP but autoloading your object class file(s).
While you have a path defined to your first file some or all of the following files don't have a path defined for their dependencies--like an Abstract class.
ZF1 files should actually have require statements and work without an auto loader. What you are missing and need is the same in your own modules files.
In your application/modules/front/models/Dbtable/Postcontent.php file you have to hint the path to the Abstract class as a require_once statement.

Related

React not recognizing Express

In my jsx file, I have the following:
var express = require('express');
var myExpress = express();
var http = require('http');
var app = http.createServer(myExpress);
var { Server } = require("socket.io");
var myio = new Server(app);
However, the browser says "Uncaught TypeError: express is not a function"
I have tried importing express with an import statement, as well as making my project a module in my package.json. What is weird is that when I use the same code in a regular js file in the same folder, it works perfectly well. This code was the code in every single one of the tutorials, so I am at a loss. Thank you.
Express is node framework you can't use it in react.
I think you need https://v5.reactrouter.com/web/guides/quick-start

How to render a Log / text file in node js through url?

I am trying to view the log File in the browser on hitting a particular url. I have the log file in my local (/logsFolder/app.log)
I tried the following codes:
Code: 1
var app = express();
var path = require('path');
var logFile = require('/logs/app');
app.use('/logs',logFile);
It threw error like
Error: Cannot find module '/logs/app'
Code :2
const app = express();
const path = require('path');
const router = express.Router();
router.get('/logFile', function(req, res){
console.log("inside logt :");
res.render('./logs/app.log');
});
can anyone Please help me to resolve.
Your log is static text file, not a javascript, nor json file, that why you can't require it. (code 1)
You are not using template engine either, that's why your code 2 didn't work, It cannot be render by itself.
You can use the built in express middleware for static files.
Try this:
app.use(express.static('logsFolder'))
Now you can access all the content of logsFolder by requesting the file name. For example: http://your-url/app.log
Or try your code 2 with res.sendFile instead of res.render

Small api on node.js

maybe someone have example for creating small API on node.js
Example, i have function this function will return string
I need call this string from postman or inside any another project with http.
What better for create this API?
Maybe someone know good guide? I worked before with node.js only like web-server
It is best practice that building API's in nodejs with express.Here is a code in which function returns a string. You need to export it. And require the file that contains exported value in your project file with the file path. Here is my function code where I am returning a string value. func.js
var string;
function myfunc(param,callback){
var str="hello";
callback(null,str);
}
myfunc('aaa',function(err,result){
if(err){
console.log(err);
}
else{
exports.string=result;
}
});
Here is my routes file and here I am calling the string from func.js and I named this file as str.js
var http = require('http');
var express = require('express');
var main = require('./func.js');//path of file
var org = main.string;
var app = express();
app.get('/str',function(req,res){
console.log(org);
res.send(org);
});
app.listen(3000,function(){
console.log("Server listening on 3000");
});
Run the code as node str.js and in postman run the url as http://localhost:3000/str and you can see the returned string in both response and in terminal.
Hope this helps...
This tutorial shows you how to build an API in Node.js with Express.
EDIT: You can skip the database part if you don't need it.

Can I pass variable to required file?

In express, I'm trying to move my minification to a requierd file:
app.js:
var app = express();
var minify = require("./minify.js");
In that file I try to set my template engine.
minify.js:
var app = express();
app.engine('html', mustacheExpress());
Later when I try to use to use the rendering engine in app.js, I get the error that no template-engine is set. It works if I run it all in the same file. I think the problem is that I declare the app-variable twice. How can I pass the app-variable into minify.js?
The problem is that you define new app variable, and you currently instantiate brand new express instance by calling express().
What you need to do is start using functions so that you can pass params (there are other methods too, but this is one that will work for you):
// app.js
var app = express();
var minify = require('./minify'); // don't include .js!
minify(app); // CALL the function that minify.js exports, passing params
// minify.js
module.exports = function(app) {
// because app comes as a parameter, it's the very same you've created in app.js
app.engine('html', mustacheExpress());
}
Again, there are many different methods and maybe proper approaches, depending on what you want to do, but this will do the job in your case. Read more about NodeJS and it's require system.
You can pass 'app' from app.js to your minify by using function in your module like Andrey said. You can do it like this too for example :
minify.js
module.exports = {
setAppEngine : function(app) {
app.engine( [...] );
}
}
And calling it like this in your app.js:
app.js
var app = express();
var minify = require("./minify.js").setAppEngine(app);
This solution is very useful because you can set and call others methods in minify.js. For example, you can do with the same code in minify.js:
app.js
var app = express();
var minify = require("./minify.js");
minify.setAppEngine(app);

java.lang.NoClassDefFoundError at runtime when using express framework and node.js

I am trying to write a sample code to create an instance of a java class and then invoke a method using that instance. I am using node-java module to do this. The code compiles without any error. However when I hit the URL which actually hits the same code then I get the class not found exception.
I have verified that the jar and it is there in the same directory as index.js and the jar also contains the class file (Application.class) for which the instance is being created.
My index.js file
var java = require("java");
java.classpath.push("demo.jar");
var express = require('express');
var router = express.Router();
var Application = java.import('Application');
/* GET home page. */
router.get('/', function(req, res) {
var application = new Application();
var resp = application.getResponse();
res.render('index', { title: resp });
});
module.exports = router;
Sorry for my english. I had the same problem. Look [https://github.com/joeferner/node-java/issues/147]
my code:
`var java = require("java");
var path=require("path");
var fs=require("fs");
console.log("ruta in directory",path.join(__dirname));
console.log("exist file:",fs.existsSync(path.resolve(__dirname,"./lib-java/lib-tgd.jar")));
java.classpath.push("commons-lang3-3.1.jar");
java.classpath.push("commons-io.jar");
java.classpath.push(path.resolve(__dirname,"./lib-java/lib-tgd.jar"));
java.classpath.push(path.resolve(__dirname,"./lib-java/jackson-annotations- 2.5.1.jar"));
java.classpath.push(path.resolve(__dirname,"./lib-java/jackson-core-2.5.1.jar"));
java.classpath.push(path.resolve(__dirname,"./lib-java/jackson-databind-2.5.1.jar"));`
with path.resolve solves the problem of the file path

Resources