Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couldn't host nodejs app with apache #2

Open
sayaligp opened this issue Oct 27, 2017 · 2 comments
Open

Couldn't host nodejs app with apache #2

sayaligp opened this issue Oct 27, 2017 · 2 comments

Comments

@sayaligp
Copy link

sayaligp commented Oct 27, 2017

I have created a small webapp using node/express & I want to host it with apache in Ubuntu. I did it using pm2(process manager for node js)
I have installed below :
- node js (version 6.11.4) ( and npm has version 3.10.10)
- pm2 (version 2.7.2)
- Apache ( version 2.4.18)

Below is the configured virtualhost for it :
( path : /etc/apache2/sites-available/adduser.com.conf)

<VirtualHost *:80>
ServerName adduser.com
ServerAlias www.adduser.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/EastAddUser

<Directory  /var/www/html/EastAddUser>
	Require local
	AllowOverride All
       	Options Indexes FollowSymLinks
</Directory>
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
	Require all granted
</Proxy>
<Location /East>
	ProxyPass http://localhost:8081  
	ProxyPassReverse http://localhost:8081
</Location>
ErrorLog /var/www/html/EastAddUser/error.log
CustomLog /var/www/html/EastAddUser/access.log combined

Kept node js application content at path : /var/www/html/EastAddUser

Below is the content of app.js file :
(Path : /var/www/html/EastAddUser/app.js)
var express = require('express');
var app = express();
var bodyParser=require('body-parser');
app.use(bodyParser.json())
const fs=require("fs");
app.use(express.static(__dirname));
var xmlOperator = require('./js/xmlOperation.js');
var server = app.listen(8081, function () {
console.log('Node server is running..');
});
app.get('/',function(req,res){
var path=__dirname+'/addUser.html';
res.sendFile(path);
})
app.post('/writeToFile', function(req, res) {
// function writes data in a file
});

Below is the content of package.json file :
(path: /var/www/html/EastAddUser/package.json )
{
"name": "eastadduser",
"version": "1.0.0",
"description": "eastadduser",
"main": "app.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start":"node app.js"
},
"author": "sayali",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"bootstrap": "^3.3.7",
"express": "^4.16.2",
"jquery": "^3.2.1",
"xml2js": "^0.4.19",
"xmlbuilder": "^9.0.4"
}
}

And html file is kept at path /var/www/html/EastAddUser/addUser.html
I have enabled the modules proxy & proxy_http, enabled this site using a2ensite command, started the app.js using pm2 (command : pm2 start app.js)

In browser, when I request to "www.adduser.com/East" , I get error as "The requested URL /East was not found on this server."

when I request for "localhost/East", then the webpage appears but it doesn't calls nodejs functions ( gets error : http://localhost/writeToFile 404 (Not Found) )

Where I did the mistake??

@rupachowrasia
Copy link
Owner

The problem is with your routes. You are trying to open www.adduser.com/East but I can not see any such route you have defined.
Try to change this
app.get('/',function(req,res){ var path=__dirname+'/addUser.html'; res.sendFile(path); })
to
app.get('/east',function(req,res){ var path=__dirname+'/addUser.html'; res.sendFile(path); })

or add a new route like -
app.get('/east', function(req,res){ // your content will go here })

It will work.

@sayaligp
Copy link
Author

Hi,
Thanks for the reply. I also changed the route. But I removed /east from my route & it worked correctly.. Thanks..
But I have another problem that is I can access my site using localhost/127.0.0.1 But I couldn't access it using the server name defined (adduser.com). When I try to access it with www.adduser.com the page gets redirected to some other page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants