在同一台服务器上(同一端口)运行Vue.js和Laravel
我正在开发一个网站,该网站使用Laravel作为后端,并使用Vue.js 2作为前端.现在每次我要运行我的网站时,我都必须使用2条命令:
I am developing a website using Laravel as backend and Vue.js 2 as frontend. Now everytime i want to run my website I have to use 2 command:
php artisan serve
这将在端口8000上运行laravel服务器
This will run the laravel server on port 8000
npm run dev
这将在端口8080上运行vue.js服务器
This will run the vue.js server on port 8080
我可以只使用一个命令在同一服务器(同一端口)上运行它们吗?
Can I just run them on the same server (same port), with just one command only??
如果您将Laravel应用程序与其中包含的Vue.js一起使用,则无需运行 npm run dev
.
If you use the Laravel app with Vue.js included in it, you don't need to run npm run dev
.
在您的情况下,您似乎前端和后端是分离的.在这种情况下,您实际上没有选择托管2个不同的端口或主机名.
In your case, you seem to have a decoupled frontend and backend. In that case, you don't really have the choice to host 2 different ports or hostname.
如果使用其他域,则可以使用相同的端口.例如,您可以将mydomain.com用作前端,将api.mydomain.com用作后端/api.
You can use the same port if you use a different domain. For example, you can use mydomain.com for the frontend and use api.mydomain.com for the backend/api.
假设您有一个webpack前端项目,则可以执行 npm run build
,它将所有文件编译为dist文件夹中的静态html文件.然后,您只需要将主机配置文件指向该文件夹即可.
Assuming you have a webpack frontend project, you can do npm run build
, which will compile all your files to static html files in the dist folder. You then only need to point your host configuration file to this folder.