AnguarJS +的node.js(网页server.js)从URL中移除#标签和服务器端支持服务器端、移除、标签、网页

2023-09-13 04:25:17 作者:孤独和酒

据我的理解,以便从angularJS删除这个标签,我需要添加以下code:

As far as i understand in order to remove the hashtag from angularJS i need to add the following code:

config(function($locationProvider) {
    $locationProvider.html5Mode(true);
}).

之后添加此code,我得到404从我的简单的服务器(Web-server.js),这是带有棱角tuotorial同一简单的服务器。

after added this code, i get 404 from my simple server (web-server.js) which is the same simple server provided with angular tuotorial.

从计算器我发现2链接,帮助我了解,我需要的路由,以支持此操作添加到我的服务器。

From the stackoverflow i found 2 links that helped me to understand that i need to add routing to my server in order to support this operation.

AngularJs无路由链路#标签?

routing清洁路径(不包括hashtag)不angularJS

好吧,我不知道如何添加网络server.js我所需要的路由

well i didn't understand how to add web-server.js the routing i needed

我的链接是previously是:/app/index.html/#cars现在他/app/index.html/car

my link was previously was: /app/index.html/#cars and now he is /app/index.html/car

应如何以适应我的新路由需要改变​​网络server.js?

how should i changed web-server.js in order to fit my new routing needs?

感谢。

推荐答案

的simlpiest并建立节点服务器路由使用的防爆preSS 框架。

The simlpiest and the fastest way to set up node server with routing is to use Express framework.

与前preSS很简单,你的web应用的框架是这样的:

With Express very simple skeleton of your webapp would look like this:

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

var app = express();

app.configure(function() {
    app.set('views', __dirname + '/app');
    app.set('view engine', 'ejs');       //EJS template engine (no HTML for now)
    app.use('/public', express.static(__dirname + '/public'));  //specify path to your static files
});

//"car", "bus", and "van" are the pages of your app
app.get('/:page(|car|bus|van)', function(req, res) {
    res.render('index', {
        page: req.params.page     //pass page to the view (just in case)
    });
});

http.createServer(app).listen(80);

然后,你必须创建 /app/index.ejs 文件,它实际上是在与一些additioal功能HTML文件。

Then you will have to create /app/index.ejs file which is in fact the html file with some additioal features.

因此​​,当你在浏览器中 /车 /总线或 /面包车您将看到 index.ejs 文件的内容。

Thus, when you navigate in your browser to /car, /bus, or /van you will see the content of index.ejs file.

希望这有助于!

修改

我刚刚修正 /:页(|汽车|巴士|面包车)部分。请尝试最后一个版本。

I've just corrected /:page(|car|bus|van) part. Please try the last version

编辑2

哦,我忘了说,你必须 NPM安装EJS 从命令行开始使用EJS模板

Oh, I forgot to say, you have to npm install ejs from command line to start using EJS templates