2015-03-23 17:55:49 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2017-03-01 18:58:27 +00:00
|
|
|
'use strict';
|
|
|
|
const connect = require('connect');
|
|
|
|
const convert = require('./convert.js');
|
|
|
|
const http = require('http');
|
|
|
|
const optimist = require('optimist');
|
|
|
|
const path = require('path');
|
|
|
|
const reactMiddleware = require('react-page-middleware');
|
2015-02-12 04:26:43 +00:00
|
|
|
|
2017-03-01 18:58:27 +00:00
|
|
|
const argv = optimist.argv;
|
2015-02-12 04:26:43 +00:00
|
|
|
|
2017-03-01 18:58:27 +00:00
|
|
|
const PROJECT_ROOT = path.resolve(__dirname, '..');
|
|
|
|
const FILE_SERVE_ROOT = path.join(PROJECT_ROOT, 'src');
|
2015-02-12 04:26:43 +00:00
|
|
|
|
2017-03-01 18:58:27 +00:00
|
|
|
let port = argv.port;
|
|
|
|
if (argv.$0.indexOf('node ./server/generate.js') !== -1) {
|
2015-02-12 04:26:43 +00:00
|
|
|
// Using a different port so that you can publish the website
|
|
|
|
// and keeping the server up at the same time.
|
|
|
|
port = 8079;
|
|
|
|
}
|
|
|
|
|
2017-03-01 18:58:27 +00:00
|
|
|
const buildOptions = {
|
2015-02-12 04:26:43 +00:00
|
|
|
projectRoot: PROJECT_ROOT,
|
|
|
|
pageRouteRoot: FILE_SERVE_ROOT,
|
|
|
|
useBrowserBuiltins: false,
|
|
|
|
logTiming: true,
|
|
|
|
useSourceMaps: true,
|
|
|
|
ignorePaths: function(p) {
|
|
|
|
return p.indexOf('__tests__') !== -1;
|
|
|
|
},
|
|
|
|
serverRender: true,
|
|
|
|
dev: argv.dev !== 'false',
|
|
|
|
static: true
|
|
|
|
};
|
|
|
|
|
2017-03-01 18:58:27 +00:00
|
|
|
const app = connect()
|
2015-02-12 04:26:43 +00:00
|
|
|
.use(function(req, res, next) {
|
|
|
|
// convert all the md files on every request. This is not optimal
|
|
|
|
// but fast enough that we don't really need to care right now.
|
2016-03-24 21:10:15 +00:00
|
|
|
if (!server.noconvert && req.url.match(/\.html|\/$/)) {
|
2017-02-07 02:16:52 +00:00
|
|
|
var extractDocs = req.url.match(/\/docs/); // Lazily extract docs.
|
|
|
|
convert({extractDocs});
|
2015-03-16 02:54:14 +00:00
|
|
|
}
|
2015-02-12 04:26:43 +00:00
|
|
|
next();
|
|
|
|
})
|
|
|
|
.use(reactMiddleware.provide(buildOptions))
|
2017-03-01 18:58:27 +00:00
|
|
|
.use(connect.static(FILE_SERVE_ROOT))
|
2016-07-12 20:42:32 +00:00
|
|
|
.use(connect.favicon(path.join(FILE_SERVE_ROOT, 'react-native', 'img', 'favicon.png')))
|
2015-02-12 04:26:43 +00:00
|
|
|
.use(connect.logger())
|
|
|
|
.use(connect.compress())
|
|
|
|
.use(connect.errorHandler());
|
|
|
|
|
2017-03-01 18:58:27 +00:00
|
|
|
const portToUse = port || 8079;
|
|
|
|
const server = http.createServer(app);
|
2015-03-27 01:10:02 +00:00
|
|
|
server.listen(portToUse, function(){
|
|
|
|
console.log('Open http://localhost:' + portToUse + '/react-native/index.html');
|
|
|
|
});
|
2015-02-12 04:26:43 +00:00
|
|
|
module.exports = server;
|