我试图分离职责,在不同的文件和模块中划分我将在服务器端的应用程序中执行的不同操作。
我遇到了一个问题,我无法理解。我尝试从启动服务器的文件(变量app )导出,在该文件中,我以以下方式存储express:
server.js
import express from 'express';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackConfig from '../webpack.config';
import path from 'path';
const app = express();
app.set('port', process.env.PORT || 3000);
app.use(webpackDevMiddleware(webpack(webpackConfig)));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '..', 'public', 'index.html'));
});
app.get('/api', (req, res) => {
res.json({api: "Woks Fine"});
});
app.listen(app.get('port'), () => {
console.log("App Start in Port", app.get('port'));
});
export default app;apiGoogleMaps.js
import app from '../server.js';
export function respuestaMensaje(apiUrl, app) {
console.log(apiUrl);
app.post(apiUrl, (req, res) => {
console.log(req.body);
});
}adressjs
import React, { Component } from 'react';
import { render } from 'react-dom';
import request from 'superagent';
import { respuestaMensaje } from '../../../src/handlers/apiGoogleMap.js';
class AddressInput extends Component{
constructor(){
super();
this.state = {
address: "",
api:"http://maps.google.com/maps/api/geocode/json?address=",
direccion: "",
latitud: "",
longitud:""
};
}
render(){
return(
<div>
<form>
<input type="text" value={this.state.address} onChange={this.updateAdress.bind(this)}/>
<button onClick={this.getAddressGeo.bind(this)}>Consultar</button>
</form>
<ul>
<li><label>Direccion:</label>{this.state.direccion}</li>
<li><label>Latitud:{this.state.latitud}</label></li>
<li><label>Longitud:{this.state.longitud}</label></li>
</ul>
</div>
)
}
updateAdress(event){
this.setState({
address: event.target.value
});
}
getAddressGeo(e){
e.preventDefault();
const apiUrl = this.state.api + this.state.address;
respuestaMensaje(apiUrl);
}
}
export default AddressInput;项目结构

出现了许多这样的错误:
[1] WARNING in ./node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js
[1] 18:11-32 Critical dependency: the request of a dependency is an expression
[1] @ ./node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js
[1] @ ./node_modules/uglifyjs-webpack-plugin/dist/uglify/minify.js
[1] @ ./node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js
[1] @ ./node_modules/uglifyjs-webpack-plugin/dist/index.js
[1] @ ./node_modules/uglifyjs-webpack-plugin/dist/cjs.js
[1] @ (webpack)/lib/WebpackOptionsDefaulter.js
[1] @ (webpack)/lib/webpack.js
[1] @ ./src/server.js
[1] @ ./src/handlers/apiGoogleMap.js
[1] @ ./src/ui/components/address.js
[1] @ ./src/ui/app.js
[1] @ ./src/ui/index.js
[1]发布于 2018-03-30 23:56:10
此错误是由server.js中用作中间件的webpack导入造成的:
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackConfig from '../webpack.config';
import path from 'path';
const app = express();
app.set('port', process.env.PORT || 3000);
app.use(webpackDevMiddleware(webpack(webpackConfig)));堆栈跟踪显示的帧如下:
[1] @ (webpack)/lib/WebpackOptionsDefaulter.js
[1] @ (webpack)/lib/webpack.js
[1] @ ./src/server.js看起来你不可能把webpack包括在webpack包里,至少不能通过进口,这要归功于webpack所使用的动态导入。
同样的错误出现在express (特别是express.view)中,用本期解释
在ExpressJS中有一个动态需求:https://github.com/expressjs/express/blob/master/lib/view.js#L78
您可以将express (和/或webpack)标记为外部依赖性,以防止它们被包含在包中(我没有使用这个库,它是从问题中链接的)。
发布于 2018-03-28 04:55:48
与其导出应用程序(整个服务器的主干网),不如在apiGoogleMaps.js文件中使用路由器并将其导出。
成功地使用app.js文件中的路由器!:D
birds.route.js
var express = require('express')
var router = express.Router()
router.get('/', function (req, res) {
res.send('Birds home page')
})
router.get('/:id', function (req, res) {
res.send(req.params.id);
})
module.exports = routerapp.js
const myBirds = require('./birds');
...
...
app.use('/birds', myBirds);https://stackoverflow.com/questions/49524157
复制相似问题