如何使用Node/Express服务我的Web应用程序? [英] How can I serve my web app with Node/Express?

查看:50
本文介绍了如何使用Node/Express服务我的Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能要问一个巨大的菜鸟问题,这是我在这里问过的最糟糕的问题之一,但是我对Node/Express感到迷茫.我只使用过Apache服务器(出于测试目的,通常使用WAMP/XAMP),所以我完全不知道要为Web应用程序提供服务的方式.

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.

我的文件夹树如下:

  • www
    • nodeserver.js
    • (更多的东西)
    • Liteconomy(我的网络应用程序)
      • js
      • css
      • 插件
      • 模板
      • index.html
      • sublime_project

      很典型,是吗?好吧,我一直在寻找如何通过简单的访问方式(例如localhost:8080/Liteconomy或localhost:8080/Liteconomy.html)为该应用程序提供服务.在那之后,我的角度路由将完成其余的工作,但我无法为该应用程序提供服务.

      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.

      我已经将此写在nodeserver.js中:

      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');
      });
      

      当我执行它并访问localhost:8080时,我得到了"Hello world",但是当我访问localhost:8080/Liteconomy时,得到了以下纯文本:"Liteconomy/index.html".如果我尝试直接访问索引资源,则会收到无法获取/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
      

      然后,如果您要访问设置和移植的URL,则可以访问.

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

      建议使用 express.static 提供静态内容.

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

      希望有帮助!

      这篇关于如何使用Node/Express服务我的Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆