rupachowrasia / node-hosting-with-apache Goto Github PK
View Code? Open in Web Editor NEWConfiguring Apache to Serve NodeJS Application
Configuring Apache to Serve NodeJS Application
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??
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.