Skip to content Skip to sidebar Skip to footer

How Can I Serve My Web App With Node/Express?

I'm probably going to ask a huge noob question, one of the worst I've ever had asked here, but I'm lost as hell with Node/Express. I've only used Apache servers (typical WAMP/XAMP

Solution 1:

Do the following, it will resolve your issue.

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var app = express();


 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({ extended: false }));
 app.use(cookieParser());

// uncomment following if you want to access your app from /liteconomy
//app.use('/liteconomy', express.static(__dirname + '/Liteconomy', {index: "index.html"}));

//This will enable you to access it form '/'
app.use('/', express.static(__dirname + '/Liteconomy', {index: "index.html"}));

// Rest of the stuff

Then if you will visit your URL that you set and port, you'll be able to access.

Using express.static is recommended way of serving static content.

Hope it helps!


Solution 2:

You get a plain text answer because you actually ask to do it with the :

app.get('/Liteconomy', function (req, res) {
   res.send('Liteconomy/index.html');
});

If you want to send a simple html file like your index.html file, you should use the "sendfile " function :

app.get('/Liteconomy', function (req, res) {
   res.sendfile(__dirname + '/Liteconomy/index.html');
});

"__dirname" represents your root directory path and then you simply put your file path.

Hope that helps !

PS : by default express come with jade and ejs template support instead of just using html. I would advise you to take a look at one of them, it can be a great help to construct your application web pages.


Post a Comment for "How Can I Serve My Web App With Node/Express?"