【发布时间】:2017-08-18 07:29:27
【问题描述】:
我正在努力学习react + express。这是我关注的文件夹结构。
app
-client
-components
-home.jsx
-nodemodules
-server
-config.js
-package.json
-routes.js
**Package.json:
{
"name": "job-application",
"version": "1.0.0",
"description": "making a website using MERN ",
"main": "nodemon config.js",
"scripts": {
"start": "node config.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/devarashetty/webApplication.git"
},
"author": "sairam",
"license": "ISC",
"bugs": {
"url": "https://github.com/devarashetty/webApplication/issues"
},
"devDependencies": {
"babel-core": "^6.0.14",
"babel-loader": "^6.0.0",
"webpack": "^1.12.2",
"webpack-dev-server": "^1.12.1"
},
"dependencies": {
"express": "3.x",
"http": "*",
"react": "*",
"react-dom": "*",
"react-router": "*",
"react-router-dom": "*",
"react-router-config": "*",
"nodemon": "*",
"mongodb": "*",
"semantic-ui-react": "*"
},
"homepage": "https://github.com/devarashetty/webApplication#readme"
}
Routes.js
import HomePage from './client/components/home';
我收到错误Unexpected token import。这背后的原因是什么。我认为导入是javascript中可用的默认函数
config.js
var http = require("http");
var mongodb = require("mongodb");
var app = require('express')();
var server = require('http').Server(app);
var routes = require("./routes.js");
var Router = require('react-router');
server.listen(8000);
app.use(function(req, res, next) {
console.log("------------", req.url);
var router = Router.create({
location: req.url,
routes: routes
});
router.run(function(Handler, state) {
var html = React.renderToString( < Handler / > )
return res.render('react_page', {
html: html
})
})
});
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/catering';
MongoClient.connect(url, function(err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', url);
db.close();
}
});
【问题讨论】:
-
@BelminBedak 我创建了一个 package.json 文件,添加了所需的依赖项并做了
npm install。尽管babel将负责将es6代码转换为javascript,因此我添加了它也是。但它仍然会发生。 -
请贴出config.js的代码
-
@HariLamichhane 我会发布它
-
@HariLamichhane 请检查我已为该 config.js 添加代码
-
我正在发布解决方案:)
标签: javascript reactjs react-router jsx