How to get THREE.JS project to work with PHP not just HTML? [duplicate] - node.js

Ok I've been playing around with nodejs, expressjs and socket.io to create some applications. But now im coming to the stage where i want to take things a bit further.
I have noticed a few node apps using PHP for twitter auth on their client side. When I attempt to rename my client.html file to client.php and restart the server it throws up a blank page with this
Cannot GET /
How do would serve php files or would i do twitter auto using js?
This is my NodeJS server.js
var http = require('http'),
express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
app.listen(1234);
console.log("server started on port :1234");

As already noted, node.js itself won't interpret php files for you. Available options are:
have nginx in front of node and reverse proxy all request you want to process in node from it
proxy php requests from node to php-capable server
spawn php process on each request in node
make fastcgi link to php-fastcgi using node fastcgi protocol parser

Uh, skip PHP entirely and integrate everyauth into your app.

PHP-EXPRESS should do. Just install the package and follow the docs.
https://www.npmjs.com/package/php-express

Related

How do I serve an Angular application from existing nodejs server?

I have an Angular 6 application and an existing nodejs api application.
So far I have used
ng serve
to run and build the angular application.
I now want to serve my angular application from the existing node js server.
How do I do that ? I can't find any documentation.
Steps:
Do ng build, It will create a dist folder which you can easily serve from node webserver framework like express, Hapi or Koa
if you are using express.js you can server angular app.use(express.static(path.join(__dirname, 'dist')));
Now use node server URL to serve angular like http://localhost:nodeport
If you are using Hapi: check this out https://hapi.dev/tutorials/servingfiles/?lang=en_US
================================basic express server================
const express = require('express');
const app = express();
const path = require("path");
const fs = require("fs");
//const bodyParser = require('body-parser');
app.use(express.static(path.join(__dirname, 'dist')));
//app.use(bodyParser.json());
//app.use('/api/v1/', require('./api/routes'));
app.listen(8080,function(err){
if(!err){
console.log("server is running at port:8080);
}
})
You have two ways of serving an Angular SPA:
Usually dev: the Webpack-run server, which is ng serve. Dynamic in the sense that any modification to a file starts a rebuild and updates the output.
Usually prod: you build all the html/js files (with ng build [...]) for the SPA to be statically served by a node server.
In your case, if you'd like to use an existing node server, it means you'll have to build the files (with ng build) and then hook up the usual node static files serving snippet in your node app.
Beware though: you'll have to do a full build each time you want to update the display. So it's ok if it's not that often, but not ok for a dev environment I guess.

How to re-route certain request url to different server in NodeJS

I have here an Angular 2 application which is certainly running on NodeJS but heres the catch, consider this ng application is in the main directory www.angular2app.com and I have 2 other sub modules which is totally not angular which is
www.angular2app.com/blog > WordPress App 1
www.angular2app.com/events > WordPress App 2
www.angular2app.com/anythingelse > Angular 2
Is there a way to handle this requests properly in NodeJS? WordPress 1 and WordPress 2 are totally different sites. There is no apache or nginx here because it's on running on MS Azure
Hope some evangelic could help me with this problem.
Thank you
You can try to use the solution similar with Virtual Directory in IIS configuration. Please consider the following test structure in Azure Web Apps.
For simple reference, my test project only contains a node.js and a php application.
The project's structure:
wwwroot/
-nodejs/
-server.js
-web.config
-php/
-index.php
The server.js is a simple node.js server script:
var http = require("http");
var server = http.createServer(function(request, response) {
response.write("Hello World!");
response.end();
});
server.listen(process.env.PORT ||1337);
console.log("Server is listening");
The web.config is a iisnode configuration file for starter nodejs application, you can find the content at https://github.com/projectkudu/kudu/wiki/Using-a-custom-web.config-for-Node-apps.
And the index.php script contents a test sentence phpinfo().
When you browse domain/nodejs, the requests are against to the node.js application.
And when you browse domain/php, the requests are against to the PHP application.
Any further concern, please feel free to let me know.

Can I use node.js server modules in webpack?

Can we import all node.js modules in webpack and create a bundle.js? Example what if I use http module and webpack application and bundle it and run in browser?
main.js
var http = require('http');
var server = http.createServer(function (req, res) {
// send output
});
server.listen(8080);
When using Node's built-in libraries with Webpack, it will automatically import a browser-compatible version when available.
You can see the entire list and matching packages in this file.
For http, you'll end up with http-browserify instead. Not everything is supported, so creating a HTTP server will not work (as this isn't possible in a browser). You can still use http to do requests as shown in the documentation.

Grunt and express.js server

Currenty I'm using grunt with karma and jasmine to run my tests etc. for my Angular app.
I want to connect this app to a mongo database and was wondering what the best way to do this is. Should I keep using grunt and just connect to a database and use it all the way, or should I use an Express server as my main server connected to the database and run the tests with grunt?
Initially I want to publish this project to heroku, and I know you can do this by just adding a static server.js (wich I do not currently have) like this.
var express = require('express');
var port = process.env.PORT || 3000;
var app = express();
app.use(express.static(__dirname + ‘/public’));
app.listen(port);
and modify the gruntfile.js with this:
tasks
grunt.registerTask('heroku',
['compass:dist', 'autoprefixer', 'imagemin']);
What is the best way to do this?
I see, I feel you have slight misconception of what grunt is. Grunt is a task runner. It will run other commands when for each task. Say for example if you can compile css or minifiy js or combine images before starting server you can do it with grunt. But that does not mean grunt can do all those by itself. It will be using other libraries for those.
If you are using grunt to do testing you internally using jasmine or karma js or something else. Same when you say grunt serve you use express internally start server. So grunt does not connect to mongodb. It is express which connects to mongodb. You can write grunt tasks which will start mongodb and start express server but grunt can not do either by its own.
Should you use grunt? Yes of course.

nodejs, expressjs serving PHP files

Ok I've been playing around with nodejs, expressjs and socket.io to create some applications. But now im coming to the stage where i want to take things a bit further.
I have noticed a few node apps using PHP for twitter auth on their client side. When I attempt to rename my client.html file to client.php and restart the server it throws up a blank page with this
Cannot GET /
How do would serve php files or would i do twitter auto using js?
This is my NodeJS server.js
var http = require('http'),
express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
app.listen(1234);
console.log("server started on port :1234");
As already noted, node.js itself won't interpret php files for you. Available options are:
have nginx in front of node and reverse proxy all request you want to process in node from it
proxy php requests from node to php-capable server
spawn php process on each request in node
make fastcgi link to php-fastcgi using node fastcgi protocol parser
Uh, skip PHP entirely and integrate everyauth into your app.
PHP-EXPRESS should do. Just install the package and follow the docs.
https://www.npmjs.com/package/php-express

Resources