Laravel与Express整合咨询:jQuery代码迁移及部署位置疑问
Hey there! Let's walk through your questions clearly since you're transitioning to Express while keeping your Laravel API up and running. I'll break down each point with practical recommendations:
1. Should I merge the two project directories?
You don't have to merge them, but it depends on your long-term workflow:
- Separate directories are great if you want to keep Laravel focused on API duties and Express as your frontend/static server. This lets you scale, update, and deploy each independently (e.g., deploy Laravel to a PHP server and Express to a Node.js host).
- Merging into a single parent folder is okay if you want to simplify CI/CD or server management, but keep their internal structures completely separate. Each project should retain its own
package.json(for Express) andcomposer.json(for Laravel) — never mix their dependencies or config files.
2. Should I install Express inside Laravel's views directory?
Absolutely not. Laravel's views folder is reserved for Blade templates and PHP-related frontend logic. Installing Express here will clutter Laravel's structure, make maintenance a nightmare, and mix two entirely different ecosystems (PHP vs. Node.js). Keep your Express project in its own dedicated directory like you're doing now.
3. Should I move my jQuery code to the Express directory and call Laravel routes from there?
Yes, this is the cleanest approach. Here's how to do it:
- Move your
scripts.jsfile to Express's static assets directory (usuallypublic/js/— create the folder if it doesn't exist). - Update your jQuery code to point to your Laravel API's full, absolute URL (e.g., replace
URL_TO_MY_LARAVELwithhttp://your-laravel-domain/api/save-properties). - If you were using Blade templates to render the frontend, convert that logic to Express (either use static HTML files in Express's
publicfolder, or use a template engine like EJS if you need dynamic content).
Example Optimized Directory Structure
- project-root (optional parent folder) - laravel-api - public - resources - ... (other Laravel core directories) - express-frontend - public - js - scripts.js // Your migrated jQuery code - index.html // Frontend page (replaces index.blade.php) - app.js - package.json
Your Existing jQuery Code (Formatted)
var _token = $('input[name="_token"]').val(); function savePropertiesSB(){ $.get( "URL_TO_OTHER_PAGE", function( data ) { $.post('URL_TO_MY_LARAVEL', {_token : _token, propertiesString: data}); }); } function savePropertiesMLS(total_pages){ var number_page = 0; var loaded = 0; var url; var callSameTime = true; var callSameTimeDetail = true; while (number_page <= total_pages) { url = 'URL_TO_OTHER_PAGE'; if (callSameTime) { $.post(url, function(data) { callSameTime = false; $(data).find('div.flexmls_connect__sr_result').each(function(){ var link_property = $(this).find('.flexmls_connect__sr_address a').attr('href'); var property = $(this); if (callSameTimeDetail) { $.post(link_property, function (res) { callSameTimeDetail = false; }).done(function(res){ sendData(propertyData(res, property)); callSameTimeDetail = true; }); } }); callSameTime = true; }); loaded++; } number_page ++; } }
Quick Tips
- Avoid hardcoding URLs! In Express, set environment variables (e.g.,
LARAVEL_API_URL=http://your-laravel-domain/api) and inject them into your frontend code. This makes deployment to dev/prod environments much smoother. - Don't forget CORS! If Laravel and Express run on different domains, install a CORS package in Laravel (like
barryvdh/laravel-cors) to allow requests from your Express frontend.
内容的提问来源于stack exchange,提问作者Alan Cordova

