首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么不能导出/导入const = express到另一个js文件

为什么不能导出/导入const = express到另一个js文件
EN

Stack Overflow用户
提问于 2018-03-28 00:04:47
回答 2查看 3.3K关注 0票数 1

我试图分离职责,在不同的文件和模块中划分我将在服务器端的应用程序中执行的不同操作。

我遇到了一个问题,我无法理解。我尝试从启动服务器的文件(变量app )导出,在该文件中,我以以下方式存储express

server.js

代码语言:javascript
复制
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

代码语言:javascript
复制
import app from '../server.js';

export function respuestaMensaje(apiUrl, app) {
    console.log(apiUrl);
    app.post(apiUrl, (req, res) => {
        console.log(req.body);
    });
}

adressjs

代码语言:javascript
复制
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;

项目结构

出现了许多这样的错误:

代码语言:javascript
复制
[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]

全错误日志

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-30 23:56:10

此错误是由server.js中用作中间件的webpack导入造成的:

代码语言:javascript
复制
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)));

堆栈跟踪显示的帧如下:

代码语言:javascript
复制
[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)标记为外部依赖性,以防止它们被包含在包中(我没有使用这个库,它是从问题中链接的)。

票数 2
EN

Stack Overflow用户

发布于 2018-03-28 04:55:48

与其导出应用程序(整个服务器的主干网),不如在apiGoogleMaps.js文件中使用路由器并将其导出。

成功地使用app.js文件中的路由器!:D

birds.route.js

代码语言:javascript
复制
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 = router

app.js

代码语言:javascript
复制
const myBirds = require('./birds');
...
...
app.use('/birds', myBirds);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49524157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档