角6部署node.js静态路径

时间:2018-11-14 06:04:10

标签: node.js angular

当我设置静态路径public文件夹并在index中设置角度public时:

app.js(node.js):

//node.js static file path
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.get('/*', (req, res) => {
  //angular index
  res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

此设置存在一些问题:

(1)我无法将所有有角度的构建文件都放在特定的文件夹中(例如放在dist文件夹中),index.html将无法获取相关文件的正确位置(像runtime.js

(2)某些文件可能存储在公用文件夹中,例如用户上传的文件。角度构建将清除文件夹(public)中的所有文件,因此我必须将其打包到另一个文件夹(ex:dist)中,然后手动将所有文件复制到public文件夹中。此设置非常不便,每次更新时,请确保清除了旧文件。


更新

app.js(node.js):

app.get('/*', (req, res) => { 
    res.sendFile(path.join(__dirname, 'public/dist', 'index.html')); 
    // or res.sendFile(path.join(__dirname, 'public', 'dist/index.html'));
});

angular.json:

"outputPath": "server/public/dist/",

角度构建

ng build --prod --deploy-url /dist/ --base-href /dist/

当我链接索引网址时:

http://localhost:3000/

角路由器将以http://localhost:3000/index的形式呈现/ index, 但node.js会呈现http://localhost:3000/dist/index

URL不能隐藏文件夹路径dist? 这是正确的设置方法吗?

1 个答案:

答案 0 :(得分:0)

用于在实时域上运行项目。请按照以下步骤操作。

步骤1:使用此命令进行构建。 ng build --prod。

步骤2:在Dist / index.html中,使用您的域名更新基本URL。

第3步:制作dist文件夹的ZIP文件。

第4步:将ZIP文件夹复制并粘贴到您各自的服务器中。您可以使用filezilla。

第5步:解压缩文件夹。

第6步:将dist文件夹名称更新为您的项目文件夹名称。

第7步:在浏览器上检查。

相关问题