Run static website using node or grunt? - javascript

I am new to node or grunt. I have developed a website using html, css and bootstrap. Now I am confused how to run this site? shall I use nodejs to serve static files or use grunt?
Most of the examples I see use grunt-watch and grunt-serve to serve files. Is watching a file necessary only during development phase and not in production right?
I would also like to minify the files(css,js etc) before I host it. I am from java background where I normally use apache tomcat to deploy and run a webapp, but just for static site I don't want to use tomcat server. How do I go about this?
Note: I am trying to deploy my project on heroku.

As an addition to T.J. Crowder anwer
To serve static html app
npm install -g http-server
http-server ./index.html
To serve nodejs app
npm install -g forever
forever start server.js
To develop static app
//grunt or gulp
//both support watchers, livereaload, minifications, bundling
//google it
To develop nodejs app
express
hapi
meanjs
...many other

Finally got my answer:
Used express js to serve static files
use npm install express --save -dev
and then in the index.js or server.js file:
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.get('/', function(request, response) {
response.render('public/');
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
So the above line app.use(express.static(__dirname + '/public')); will serve static files like html,css etc from this folder (i.e public)
and then in the command prompt: node index.js will start the server

Related

How to deploy a Vue.js application on Node.js server

I have a dist folder containing CSS, fonts, JS folder and an index.html file minimized for Vue.js, ready to deploy and use. I want to use Node.js to run this application. How can I set this up to just run npm run server and have it deployed on a specific port requested? Not sure how to structure this or if I need to build it in a specific way to run this Vue app. Any help would be greatly appreciated.
Since Vue is only a frontend library, the easiest way to host it and do things like serve up assets is to create a simple Express friendly script that you can use to start a mini-web server. Read up quickly on Express if you haven’t already. After that, add express:
npm install express --save
Now add a server.js file to your project’s root directory :
// server.js
var express = require('express');
var path = require('path');
var serveStatic = require('serve-static');
app = express();
app.use(serveStatic(__dirname + "/dist"));
var port = process.env.PORT || 5000;
var hostname = '127.0.0.1';
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
after that you could run :
node server
and your project will be served at the given host and port
Assuming that you have already the dist directory, if you don't have it run :
npm run build
in order to generate it

How to prepare an angular.js app to deploy to Heroku?

I have built an Angular.js quiz app by following a video. I don't understand the workings of the app yet, but it works fine with Mozilla Developer Edition, not with other browsers because I run it locally. My app's root directory contains the main HTML file, a JSON file, a folder for the CSS file, a folder for the images, and a folder for Angular.js files.
When I try to deploy the app to Heroku, it gives me an error message saying 'No default language could be detected'. It has something to do with build packs. I am unable to figure it out. Here is the link to my repo on GitHub:
My repo on GitHub
Heroku supports node server so create a basic node server for your angular application
Install NodeJS and type npm init from project folder to initialise the package.json file.
Install these packages gzippo, express, morgan. Run this command from terminal.
npm install --save gzippo express morgan
server.js
var gzippo = require('gzippo');
var express = require('express');
var morgan = require('morgan');
var app = express();
app.use(morgan('dev'));
app.use(gzippo.staticGzip("" + __dirname + "/"));
app.listen(process.env.PORT || 5000);
Create a Procfile (remember file without any extension)
Procfile
web: node server.js
Now try deploying your branch on Heroku

client - server architecture structure (with node js) - where to put the index.html file?

I have built a server client app (nodejs - on the server, angular - on the client).
I used to have all my code in one project (one folder) but then I figured it makes more sense for me to divide it, by logic, to a server folder and to a client folder.
I aspire for disconnecting all of the dependencies between the server code and the client code, in the sense that each side can stand alone by its own.
Questions:
where do I put the index.html?
it is obviously related to the client side but the server (expressJS) is loading it when the server.
lets says I put this index.html file in the client folder like in the picture below..
how can I make the server.js be aware of it?
this code doesn't work:
var app = express();
app.use(bodyParser.json());
app.use('/',express.static(__dirname));
app.use('/scripts', express.static(path.join(__dirname,
'node_modules')));
app.get('/', function (req, res) {
res.sendFile(express.static(path.join(__dirname + '../client/index.html')));
});
Usually with express apps, you would have a "public" folder that contains your client assets, index.html being one of them.
app.use('/',express.static(path.join(__dirname, '/public')));
then, run the node server, open a browser and point it to that server and express will serve up "index.html" from the "public" folder.
Steps to fix
Rename client folder to public
Drag public inside server folder so its like server/public
change app.use('/',express.static(__dirname)); to app.use('/', express.static(path.join(__dirname, '/public')));
Please note: app.use('/',express.static(__dirname)); should be avoided, you're serving up your whole server directory to the public.
If you want to have a better look at project layouts.
Have a look at yoeman.io, specifically it has project templates for express.
http://yeoman.io/
Yeoman helps you to kickstart new projects, prescribing best practices
and tools to help you stay productive.
var app = express();
app.use(bodyParser.json());
app.use('/',express.static(__dirname));
//it will serve all files in client directory under the '/' route
app.use('/', express.static(path.join(__dirname, '/client'));
});
//and the client side scripts installed through npm will be served under '/scripts' route
app.use('/scripts', express.static(path.join(__dirname, '/client/node_modules')))
It will be good to serve the client side modules in separate folder i.e. to have node_module folder in the client directory as well, there you should install all modules which should be load from the client side. Besides this the client folder seems public, so it is worth to serve the whole folder.
If you want to install modules for client side then go to '/client/node_modules' directory in the terminal and run the npm. So if you are linux, then:
cd client/
npm init
npm install YOUR_DESIRED_MODULE
In case of modules for server side install modules in your root directory

Set up proxy server for create react app

I have started a react application using create-react-app and ran the npm run eject script to gain access to all files. I afterwards installed express and created server.js file that sits on same level as package.json file
these are server.js file contents:
const express = require('express');
const app = express;
app.set('port', 3031);
if(process.env.NODE_ENV === 'production') {
app.use(express.static('build'));
}
app.listen(app.get('port'), () => {
console.log(`Server started at: http://localhost:${app.get('port')}/`);
})
Nothing crazy here, just setting up for future api proxies where I need to use secrets and as I don't want to expose my api.
after this I added a "proxy": "http://localhost:3001/" to my package.json file. I am now stuck as I need to figure out how to start my server correctly and use this server.js file in development mode and afterwards in production.
Ideally It would also be good if we could use more that one proxy i.e. /api and /api2
You didn't have to eject to run your server.js. You can just run it with node server.js together with create-react-app.
You can still do npm start even after ejecting to start your dev server.
To run /api1 and /api2, you just have to handle it in your server.js file and it should work just fine. You need to match the port in your server.js and the one in proxy settings inside package.json - in this case, it should be "proxy": "http://localhost:3031"

How to setup simple static server where path '/' points to index.html within dirrectory?

There might be a very simple solution to this question, but I am not able to find answer online and due to my practice with node I can't figure it out either.
I'm trying to set up a simple server.js file that listens on port 80 and serves /dist/index.html file when users enter root address, so example.com
This is my project structure
dist/
index.html
bundle.js
node-modules/
package.json
server.js
You can create a static server with express:
server.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/dist'));
app.listen(8080, function() {
console.log('Listening on port: ' + 80);
});
You simply run node server.js to get the static server. This app can also be deployed.
http-server is a simple method of serving from your file system. Install that, then just run http-server -p X in the command line in your project folder, substituting your port number for X

Categories

Resources