前后端不分离时,前端页面需要在浏览器上运行,本地打开的话一般是file协议,如果代码中存在http或https链接,页面就无法正常打开,这时候就需要在本地开启一个服务
使用node.js搭建本地服务的2种方式
- http-server
全局安装命令:npm install http-server -g
进入项目文件夹执行: http-server (禁用缓存可以用:http-server -c-1)
- anywhere
全局安装命令:npm install anywhere -g
进入项目文件夹执行:anywhere -p 8080