rupachowrasia / node-hosting-with-apache

Configuring Apache to Serve NodeJS Application
3 stars 0 forks source link

Couldn't host nodejs app with apache #2

Open sayaligp opened 7 years ago

sayaligp commented 7 years ago

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 :

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 commented 7 years ago

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 commented 7 years ago

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.