简体   繁体   English

如何让快速路由到 angularjs 文件工作

[英]how to get Express routes to angularjs files working

background背景

I am trying to use the file uploads using angularjs and trying to save it with nodejs in the file.我正在尝试使用 angularjs 上传文件,并尝试使用 nodejs 将其保存在文件中。 This whole setup I am trying to do it in my local server.这整个设置我试图在我的本地服务器上完成。

My index file to angular js part lies inside a folder called portal/standards.html我的 angular js 部分的索引文件位于名为 portal/standards.html 的文件夹中

I am trying to start the server using express with a file in root directory server.js, below is the redirect that I am trying to do.我正在尝试使用根目录 server.js 中的文件使用 express 启动服务器,下面是我正在尝试执行的重定向。

Server.js服务器.js

app.get('/', function(req, res) {
  res.sendFile('portal/standards.html' , { root : __dirname});
});

Problem问题

The standards.html which works fine when I do a live-server doesn't work fine when I use the above redirect from the express server.当我使用上述来自 express 服务器的重定向时,在我做实时服务器时可以正常工作的standards.html 不能正常工作。

I am new to nodejs server side.我是 nodejs 服务器端的新手。 below are my errors, the path to my css and js files are broken for some reason(though it works fine when I load it directly)下面是我的错误,我的 css 和 js 文件的路径由于某种原因被破坏了(尽管当我直接加载它时它工作正常)

错误画面

my html我的 HTML

 <link href="../libraries/common/bootstrap.css" rel="stylesheet">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="../libraries/common/normalize.css" />
  <link rel="stylesheet" href="../libraries/floating-label/floating-label.css" />
  <link rel="stylesheet" href="../libraries/top-menu/yamm.css">
  <link href="momtest/portal/css/portal.css" rel="stylesheet">
  <link href="css/template-wizard.css" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/github.min.css">

文件夹结构

You can point express at your static files directory:您可以将 express 指向您的静态文件目录:

app.use(express.static('./portal/'));
// Optional any deep link calls should return index.html
app.use('/*', express.static('index.html'));

You should only put static assets in the directory you want to serve those assets from - do not include the php files you are currently including so that you are dividing your directory structure up logically.您应该只将静态资产放在您想要提供这些资产的目录中 - 不要包括您当前包含的 php 文件,以便您在逻辑上划分目录结构。

Dividing it up logically meaning you have one directory (and any subdirectories you need) for static files (ie code that runs in the browser) and server files.从逻辑上划分它意味着您有一个目录(以及您需要的任何子目录)用于静态文件(即在浏览器中运行的代码)和服务器文件。 A common app structure is (filenames are just examples):常见的应用程序结构是(文件名只是示例):

myApp我的应用程序
--src --src
--client - 客户
--app - 应用程序
--angular-module1 --angular-module1
--angular-module2 --angular-module2
--etc... - 等等...
--server - 服务器
app.js应用程序.js
etc...等等...

This is just an example.这只是一个例子。 There are divided opinions on how to structure express and angular apps that you can research and then decide which approach is best for your project.关于如何构建 express 和 angular 应用程序,您可以研究然后决定哪种方法最适合您的项目,存在不同意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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