Node.js 从另一台计算机发布对Express.js api的请求

Node.js 从另一台计算机发布对Express.js api的请求,node.js,angular,express,Node.js,Angular,Express,我有一台运行在localhost:3000上的服务器,当我试图在浏览器中访问localhost:3000时,我将app.js设置为使用angular路由器 (示例:app.use('/',express.static(path.join(uu dirname,'/dist/Client'));) 当我向api发出post请求时,我会: const headers = new Headers({ 'Content-Type': 'application/json' }); const opt

我有一台运行在localhost:3000上的服务器,当我试图在浏览器中访问localhost:3000时,我将app.js设置为使用angular路由器
(示例:
app.use('/',express.static(path.join(uu dirname,'/dist/Client'));

当我向api发出post请求时,我会:

const headers = new Headers({
  'Content-Type': 'application/json'
});

const options = new RequestOptions({
   headers: headers
});

this.http.post('http://localhost:3000/api/someAction',{body},options)
.toPromise()
.then(//function)
直到现在,一切都是正确的,但我如何才能使我的服务器可以从另一台计算机访问。例如,如果同一网络上的另一台计算机知道服务器的专用IP地址,我希望他在导航到例如192.168.1.10:3000时能够访问我的应用程序。现在我可以访问,但是我所有的http请求都失败了,我有以下错误

Access to XMLHttpRequest at 'http://localhost:3000/api/someFunction' 
from origin 'http://192.168.1.10:3000' has been blocked by CORS policy: 
Response to preflight request doesn't pass access control check: It does not 
have HTTP ok status.
在app.js中,我有以下内容:

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
  next();
});

这是您收到的CORS错误消息

您可以使用Express CORS中间件允许从其他来源调用服务器。确保选择正确的选项以允许从其他电脑拨打电话

还要确保将CORS中间件放在应用程序定义的顶部

const app = express();
app.use(cors({ origin: "*" })); // Do this first
//... the rest of your routes and handlers and middlewares
从您的本地机器上,在那里一切正常,打开您的Chrome开发工具,并确保
选项
调用被成功执行,并且返回了正确的头


最后,从安全的角度来看,为您的生产环境尽可能删除或限制CORS选项。仅在开发和测试期间使用灵活的CORS策略

这是您收到的CORS错误消息

您可以使用Express CORS中间件允许从其他来源调用服务器。确保选择正确的选项以允许从其他电脑拨打电话

还要确保将CORS中间件放在应用程序定义的顶部

const app = express();
app.use(cors({ origin: "*" })); // Do this first
//... the rest of your routes and handlers and middlewares
从您的本地机器上,在那里一切正常,打开您的Chrome开发工具,并确保
选项
调用被成功执行,并且返回了正确的头

最后,从安全的角度来看,为您的生产环境尽可能删除或限制CORS选项。仅在开发和测试期间使用灵活的CORS策略