How to serve an Angular 2 dist folder index.html

非 Y 不嫁゛ 提交于 2019-11-28 20:20:07

You can use http-server for doing so. First of all generate a build using the command ng build --prod --aot --output-hashing=none. This will create a dist folder in your directory structure.

After this, run http-server ./dist, which will start serving your project from dist folder.

Make sure you have installed http-server globally using

npm install http-server -g

For reference, see https://www.npmjs.com/package/http-server

You need a server to serve your generated build.

I am using http-server. Install http-server using:

npm install -g http-server

now go inside your dist folder and run this command

http-server

as shown here:

Check http://localhost:8080 in your browser

At least for Angular apps, angular-http-server seems to be a nicer option.

First install it with

npm install angular-http-server -g # or yarn global add angular-http-server

Then execute it:

angular-http-server --path path/to/dist/folder

Look at the repo for more information about usage.

PS: According the author, it should also work with other SPA frameworks (React, Vue and so forth).

A little tips

so you avoid to install globally

install in your root

npm i http-server

in your package.json

"scripts": {
    "pwa": "http-server ./dist"
  }

than

npm run pwa 

I serve the dist folder with the Angular CLI...

ng serve --prod=true

When true, sets the build configuration to the production target. All builds make use of bundling and limited tree-shaking. A production build also runs limited dead code elimination.

https://angular.io/cli/serve

ng serve will work as normal, and it doesn't require a prior build. It generates files in memory, and has some additional features like auto reload.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!