简体   繁体   中英

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 for testing purposes), so I have absolutely no idea on what I have to do to serve my web app.

My folder tree is the following:

  • www
    • nodeserver.js
    • (more things)
    • Liteconomy (my web app)
      • js
      • css
      • plugins
      • templates
      • index.html
      • sublime_project

Pretty typical, huh? Well, I've been searching how to serve this app with a simple access like localhost:8080/Liteconomy, or localhost:8080/Liteconomy.html. After that, my angular routing would do the rest, but I just can't serve the app.

I've got this written in nodeserver.js:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(8080, function () {
  console.log('Example app listening on port 8080!');
});

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

When I execute it and access to localhost:8080, I get the "Hello world", but when I go to localhost:8080/Liteconomy, I get the following plain text: "Liteconomy/index.html". If I try to access to the index resource directly, I get a "Cannot GET /Liteconomy/index.html" error.

I also tried using the static thingy, but didn't work either.

What am I doing wrong here? I guess I'm just missing something very important.

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM