2017-03-25 2 views
0

react + express을 배우려고합니다. 다음은 내가 따르고있는 폴더 구조입니다.routes.js에서 예기치 않은 토큰 가져 오기

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 무엇입니까 함수는 자바 스크립트에서 availble

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(); 
    } 
}); 
+0

@BelminBedak 필자는 package.json 파일을 만들고 필요한 종속성을 추가하고'npm install'을했습니다.'babel'은'es6' 코드를'javascript'로 변환 할 것이기 때문에 나는 또한 그것을 추가했습니다.하지만 아직도 일어난다. –

+0

config.js 코드를 게시하십시오. –

+0

@HariLamichhane 게시 할 예정입니다. –

답변

1

이 작동하는 솔루션이며 최선의 해결책 : 다음과 같이 당신의 package.json 변화 start

먼저하지 않을 수 있습니다.

"start": "node ./bin/www" 

는 이제 내부 www라는 파일 bin라는 폴더를 생성 [참고 :이 확장자가없는] www

#!/usr/bin/env node 
require("babel-register")({ 
    presets: ["es2015", "react"], 
}); 
var app = require('../app'); 
app.set('port', process.env.PORT || 3000); 
app.listen(app.get('port')); 

넷째 내부

넣어 다음 코드를, 이름 바꾸기 config.js ~ app.js 및 파일 끝에 module.exports = app을 추가하십시오.

"babel-cli": "^6.18.0", 
"babel-core": "^6.18.2", 
"babel-loader": "^6.2.8", 
"babel-preset-es2015": "^6.9.0", 
"babel-preset-react": "^6.11.1", 

이제 위에서 사용 된 파일을 제외하고 다른 파일에 import을 사용할 수 있습니다 [새로운 될 수있는 버전] 당신의 package.json 에서 이러한 종속성이 있는지 확인합니다.

관련 문제