2017-11-05 8 views
0

私は同形反応のアプリケーションを持っています。 app/src /ディレクトリにserver.jsファイルがあります。ES6でfirebaseのisomorphic single pageアプリを実行できない

Server.js

import path from 'path'; 
import express from 'express'; 
import cookieParser from 'cookie-parser'; 
import bodyParser from 'body-parser'; 
import expressJwt, { UnauthorizedError as Jwt401Error } from 'express-jwt'; 
import nodeFetch from 'node-fetch'; 
import React from 'react'; 
import ReactDOM from 'react-dom/server'; 
import PrettyError from 'pretty-error'; 
import App from './components/App'; 
import Html from './components/Html'; 
import { ErrorPageWithoutStyle } from './routes/error/ErrorPage'; 
import errorPageStyle from './routes/error/ErrorPage.css'; 
import createFetch from './createFetch'; 
import router from './router'; 
import assets from './assets.json'; // eslint-disable-line import/no-unresolved 
import configureStore from './store/configureStore'; 
import { setRuntimeVariable } from './actions/runtime'; 
import config from './config'; 

const app = express(); 

// 
// Tell any CSS tooling (such as Material UI) to use all vendor prefixes if the 
// user agent is not known. 
// ----------------------------------------------------------------------------- 
global.navigator = global.navigator || {}; 
global.navigator.userAgent = global.navigator.userAgent || 'all'; 

// 
// Register Node.js middleware 
// ----------------------------------------------------------------------------- 
app.use(express.static(path.resolve(__dirname, 'public'))); 
app.use(cookieParser()); 
app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json()); 

// 
// Authentication 
// ----------------------------------------------------------------------------- 
app.use(
    expressJwt({ 
    secret: config.auth.jwt.secret, 
    credentialsRequired: false, 
    getToken: req => req.cookies.id_token, 
    }), 
); 
// Error handler for express-jwt 
app.use((err, req, res, next) => { 
    // eslint-disable-line no-unused-vars 
    if (err instanceof Jwt401Error) { 
    console.error('[express-jwt-error]', req.cookies.id_token); 
    // `clearCookie`, otherwise user can't use web-app until cookie expires 
    res.clearCookie('id_token'); 
    } 
    next(err); 
}); 

if (__DEV__) { 
    app.enable('trust proxy'); 
} 

// 
// Register server-side rendering middleware 
// ----------------------------------------------------------------------------- 
app.get('*', async (req, res, next) => { 
    try { 
    const css = new Set(); 

    // Universal HTTP client 
    const fetch = createFetch(nodeFetch, { 
     baseUrl: config.api.serverUrl, 
     cookie: req.headers.cookie, 
    }); 

    const initialState = { 
     user: req.user || null, 
    }; 

    const store = configureStore(initialState, { 
     fetch, 
     // I should not use `history` on server.. but how I do redirection? follow universal-router 
    }); 

    store.dispatch(
     setRuntimeVariable({ 
     name: 'initialNow', 
     value: Date.now(), 
     }), 
    ); 

    // Global (context) variables that can be easily accessed from any React component 
    // https://facebook.github.io/react/docs/context.html 
    const context = { 
     // Enables critical path CSS rendering 
     // https://github.com/kriasoft/isomorphic-style-loader 
     insertCss: (...styles) => { 
     // eslint-disable-next-line no-underscore-dangle 
     styles.forEach(style => css.add(style._getCss())); 
     }, 
     fetch, 
     // You can access redux through react-redux connect 
     store, 
     storeSubscription: null, 
    }; 

    const route = await router.resolve({ 
     ...context, 
     pathname: req.path, 
     query: req.query, 
    }); 

    if (route.redirect) { 
     res.redirect(route.status || 302, route.redirect); 
     return; 
    } 

    const data = { ...route }; 
    data.children = ReactDOM.renderToString(
     <App context={context} store={store}> 
     {route.component} 
     </App>, 
    ); 
    data.styles = [{ id: 'css', cssText: [...css].join('') }]; 
    data.scripts = [assets.vendor.js]; 
    if (route.chunks) { 
     data.scripts.push(...route.chunks.map(chunk => assets[chunk].js)); 
    } 
    data.scripts.push(assets.client.js); 
    data.app = { 
     apiUrl: config.api.clientUrl, 
     state: context.store.getState(), 
    }; 

    const html = ReactDOM.renderToStaticMarkup(<Html {...data} />); 
    res.status(route.status || 200); 
    res.send(`<!doctype html>${html}`); 
    } catch (err) { 
    next(err); 
    } 
}); 

// 
// Error handling 
// ----------------------------------------------------------------------------- 
const pe = new PrettyError(); 
pe.skipNodeFiles(); 
pe.skipPackage('express'); 

// eslint-disable-next-line no-unused-vars 
app.use((err, req, res, next) => { 
    console.error(pe.render(err)); 
    const html = ReactDOM.renderToStaticMarkup(
    <Html 
     title="Internal Server Error" 
     description={err.message} 
     styles={[{ id: 'css', cssText: errorPageStyle._getCss() }]} // eslint-disable-line no-underscore-dangle 
    > 
     {ReactDOM.renderToString(<ErrorPageWithoutStyle error={err} />)} 
    </Html>, 
); 
    res.status(err.status || 500); 
    res.send(`<!doctype html>${html}`); 
}); 

// 
// Launch the server 
// ----------------------------------------------------------------------------- 
if (!module.hot) { 
    app.listen(config.port,() => { 
    console.info(`The server is running at http://localhost:${config.port}/`); 
    }); 
} 

// 
// Hot Module Replacement 
// ----------------------------------------------------------------------------- 
if (module.hot) { 
    app.hot = module.hot; 
    module.hot.accept('./router'); 
} 

export default app; 

私はfirebaseを使用して私のアプリを展開します。アプリがアプリ/機能/すなわちsrcと機能は同じ親ディレクトリを持つ内部で定義firebase機能です

{ 
    "database": { 
    "rules": "database.rules.json" 
    }, 
    "hosting": { 
    "public": "build", 
    "ignore": [ 
     "firebase.json", 
     "**/.*", 
     "**/node_modules/**" 
    ], 
    "rewrites": [ 
     { 
     "source": "**", 
     "function": "app" 
     } 
    ] 
    } 
} 

- そのために私はそうのようなセットアップfirebase.jsonを持っています。 関数のディレクトリには、正しく構成されたnode_modulesがあり、他の関数についてもテストされています。

問題 -

import app from '../src/server'; 
import functions from 'firebase-functions'; 

// // Create and Deploy Your First Cloud Functions 
// // https://firebase.google.com/docs/functions/write-firebase-functions 
// 
// exports.helloWorld = functions.https.onRequest((request, response) => { 
// response.send("Hello from Firebase!"); 
// }); 

exports.app = functions.https.onRequest(app); 

FirebaseはそれがES6をサポートdoesntのことを不平を言っている - アプリ/関数内私のindex.jsファイルがそうのようなものです。この機能をES6で動作させるにはどうすればよいですか? function/index.jsファイルをES5に変更するだけでは、必要なものが解決されたときにうまくいくとは思えないので、内部のすべてがES5になると期待しています。

答えて

0

この問題の解決方法が見つかりました。

関数でディレクトリを作成する/「ビルドする」。

次のような機能/ index.jsを変更

を構築/関数に(自分のビルドファイルが存在する場所)あなたの./distや./buildのすべての内容をコピーし、プロジェクトをビルドします -

const app = require('./build/bundle.js').default; 
const functions = require('firebase-functions'); 

// // Create and Deploy Your First Cloud Functions 
// // https://firebase.google.com/docs/functions/write-firebase-functions 
// 
// exports.helloWorld = functions.https.onRequest((request, response) => { 
// response.send("Hello from Firebase!"); 
// }); 

exports.app = functions.https.onRequest(app); 

魅力的な作品

関連する問題