语法错误:意外令牌'{'。导入调用只需要一个参数

时间:2019-05-28 10:07:15

标签: javascript html node.js nodejs-server

我正在创建一个nodejs服务器。当它在终端中运行并打开Safari / Chrome时,出现错误。我尝试在Heroku上进行部署,但又收到另一条错误消息。 可能是什么问题?


    //Server.js file

    //Module include
    var mysql = require('mysql');
    var express = require('express');
    const path = require('path');
    const app = express();
    const router = express.Router();
    const port = process.env.PORT || 3000;

    var items = {};

    //Database connection info
    var con = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        database: 'products',
        password: '12345',
    });

    //Create the database connection
    /*con.connect(function(err) {
        if (err) throw err;
        console.log("Connected!");
        getData();
    })*/

    //Load index.html file
    router.get('*', (request, response)=>{
        console.log(request.query);
        response.sendFile(__dirname +'/index.html');
    });

    //Add css file for index.html
    app.use(express.static(path.join(__dirname, '/')));

    //Add the router
    app.use('*', router);
    app.listen(port, () => {
        console.log("Server is running!");
    });

    //Get database data
    function getData() {
        var query = "SELECT * FROM product";
        con.query(query, function(err, result){
            if (err) throw err;
            items = result;
            console.log(items[0].name);
        });
    }

    module.exports = items;


//main.js file

import { items } from './server.js';


/*Products items*/
function generateItems() {

    var itemNum = 0;
    do {
        var htmlText = "<div class='overlay'>" +
            "<div class='overlay-text'>" +
                "<h2>" + items[itemNum].name + "</h2>" +
                "<p>Ár:" + items[itemNum].price + "Ft</p>" +
                "<input type='button' class='kosarbaRakomBtn' value='Kosárba rakom' onClick='addShoppingCart'>" +
                "<i class='fas fa-search zoom-picture-icon'>" +
            "</div>" +
        "</div>";
        var div = document.createElement('div');

        div.style.backgroundImage = "url(images/img01.jpg)";
        div.style.color = "#fff";
        div.className = "items";
        div.innerHTML = htmlText;

        document.getElementById('products-container').appendChild(div);

        itemNum++;

    } while (itemNum < items.length);
}

当我在Terminal上运行并打开Safari时:SyntaxError:意外令牌'{'。 import调用只需要一个参数。

当我在Heroku上运行时:

2019-05-28T01:23:29.028158+00:00 app[web.1]: npm ERR! Failed at the webshop@1.0.0 start script.
2019-05-28T01:23:29.028275+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-05-28T01:23:29.035245+00:00 app[web.1]: 
2019-05-28T01:23:29.035449+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-05-28T01:23:29.035533+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-05-28T01_23_29_029Z-debug.log
2019-05-28T01:25:17.350048+00:00 heroku[web.1]: State changed from crashed to starting
2019-05-28T01:25:18.981559+00:00 heroku[web.1]: Starting process with command `npm start`
2019-05-28T01:25:20.976274+00:00 heroku[web.1]: State changed from starting to crashed
2019-05-28T01:25:20.614684+00:00 app[web.1]: 
2019-05-28T01:25:20.614699+00:00 app[web.1]: > webshop@1.0.0 start /app
2019-05-28T01:25:20.614700+00:00 app[web.1]: > node server.js
2019-05-28T01:25:20.614701+00:00 app[web.1]: 
2019-05-28T01:25:20.957549+00:00 heroku[web.1]: Process exited with status 1
2019-05-28T01:25:20.887713+00:00 app[web.1]: /app/server.js:21
2019-05-28T01:25:20.887735+00:00 app[web.1]: if (err) throw err;
2019-05-28T01:25:20.887736+00:00 app[web.1]: ^
2019-05-28T01:25:20.887737+00:00 app[web.1]: 
2019-05-28T01:25:20.887739+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:3306
2019-05-28T01:25:20.887740+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
2019-05-28T01:25:20.887741+00:00 app[web.1]: --------------------
2019-05-28T01:25:20.887742+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
2019-05-28T01:25:20.887743+00:00 app[web.1]: at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
2019-05-28T01:25:20.887744+00:00 app[web.1]: at Connection.connect (/app/node_modules/mysql/lib/Connection.js:119:18)
2019-05-28T01:25:20.887745+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:20:5)
2019-05-28T01:25:20.887746+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:701:30)
2019-05-28T01:25:20.887747+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
2019-05-28T01:25:20.887748+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:600:32)
2019-05-28T01:25:20.887749+00:00 app[web.1]: at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
2019-05-28T01:25:20.887751+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:531:3)
2019-05-28T01:25:20.887752+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
2019-05-28T01:25:20.894896+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-05-28T01:25:20.895255+00:00 app[web.1]: npm ERR! errno 1
2019-05-28T01:25:20.896282+00:00 app[web.1]: npm ERR! webshop@1.0.0 start: `node server.js`
2019-05-28T01:25:20.896377+00:00 app[web.1]: npm ERR! Exit status 1
2019-05-28T01:25:20.896591+00:00 app[web.1]: npm ERR!
2019-05-28T01:25:20.896725+00:00 app[web.1]: npm ERR! Failed at the webshop@1.0.0 start script.
2019-05-28T01:25:20.896864+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-05-28T01:25:20.903364+00:00 app[web.1]: 
2019-05-28T01:25:20.903504+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-05-28T01:25:20.903591+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-05-28T01_25_20_898Z-debug.log
2019-05-28T01:29:31.000000+00:00 app[api]: Build started by user example@gmail.com
2019-05-28T01:29:45.056073+00:00 app[api]: Release v20 created by user example@gmail.com
2019-05-28T01:29:45.056073+00:00 app[api]: Deploy be5a1719 by user example@gmail.com
2019-05-28T01:29:45.000000+00:00 app[api]: Build succeeded
2019-05-28T01:29:47.094233+00:00 heroku[web.1]: State changed from crashed to starting
2019-05-28T01:29:49.415791+00:00 heroku[web.1]: Starting process with command `npm start`
2019-05-28T01:29:51.935864+00:00 app[web.1]: 
2019-05-28T01:29:51.935876+00:00 app[web.1]: > webshop@1.0.0 start /app
2019-05-28T01:29:51.935877+00:00 app[web.1]: > node server.js
2019-05-28T01:29:51.935878+00:00 app[web.1]: 
2019-05-28T01:29:52.348522+00:00 app[web.1]: Server is running!
2019-05-28T01:30:49.607104+00:00 heroku[web.1]: State changed from starting to crashed
2019-05-28T01:30:49.480066+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-05-28T01:30:49.480160+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-05-28T01:30:49.582564+00:00 heroku[web.1]: Process exited with status 137
2019-05-28T01:30:51.343311+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=mineralfever.herokuapp.com request_id=a7f0e295-53ec-4038-ade6-76f79786f8d5 fwd="62.165.197.25" dyno= connect= service= status=503 bytes= protocol=https
2019-05-28T01:30:56.792963+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=mineralfever.herokuapp.com request_id=111e7908-9507-47d9-9151-3b483a6683d1 fwd="62.165.197.25" dyno= connect= service= status=503 bytes= protocol=https
2019-05-28T01:32:49.000000+00:00 app[api]: Build started by user example@gmail.com
2019-05-28T01:33:02.711167+00:00 app[api]: Deploy 299419c7 by user example@gmail.com
2019-05-28T01:33:02.711167+00:00 app[api]: Release v21 created by user example@gmail.com
2019-05-28T01:33:03.000000+00:00 app[api]: Build succeeded
2019-05-28T01:33:04.025399+00:00 heroku[web.1]: State changed from crashed to starting
2019-05-28T01:33:05.927415+00:00 heroku[web.1]: Starting process with command `npm start`
2019-05-28T01:33:08.262629+00:00 app[web.1]: 
2019-05-28T01:33:08.262653+00:00 app[web.1]: > webshop@1.0.0 start /app
2019-05-28T01:33:08.262655+00:00 app[web.1]: > node server.js
2019-05-28T01:33:08.262656+00:00 app[web.1]: 
2019-05-28T01:33:08.561934+00:00 app[web.1]: Server is running!
2019-05-28T01:33:09.630373+00:00 heroku[web.1]: State changed from starting to up
2019-05-28T01:35:58.000000+00:00 app[api]: Build started by user example@gmail.com
2019-05-28T01:36:12.065424+00:00 app[api]: Deploy 848fd756 by user example@gmail.com
2019-05-28T01:36:12.065424+00:00 app[api]: Release v22 created by user example@gmail.com
2019-05-28T01:36:12.000000+00:00 app[api]: Build succeeded
2019-05-28T01:36:13.443902+00:00 heroku[web.1]: Restarting
2019-05-28T01:36:13.739890+00:00 heroku[web.1]: State changed from up to starting
2019-05-28T01:36:14.865425+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-05-28T01:36:14.991076+00:00 heroku[web.1]: Process exited with status 143
2019-05-28T01:36:15.584433+00:00 heroku[web.1]: Starting process with command `npm start`
2019-05-28T01:36:17.866237+00:00 heroku[web.1]: State changed from starting to up
2019-05-28T01:36:17.278634+00:00 app[web.1]: 
2019-05-28T01:36:17.278651+00:00 app[web.1]: > webshop@1.0.0 start /app
2019-05-28T01:36:17.278652+00:00 app[web.1]: > node server.js
2019-05-28T01:36:17.278653+00:00 app[web.1]: 
2019-05-28T01:36:17.544825+00:00 app[web.1]: Server is running!
2019-05-28T02:07:20.223592+00:00 heroku[web.1]: Idling
2019-05-28T02:07:20.303594+00:00 heroku[web.1]: State changed from up to down
2019-05-28T02:07:21.239210+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-05-28T02:07:21.305811+00:00 heroku[web.1]: Process exited with status 143
2019-05-28T08:52:22.000000+00:00 app[api]: Build started by user example@gmail.com
2019-05-28T08:52:36.858000+00:00 app[api]: Deploy ba242f9a by user example@gmail.com
2019-05-28T08:52:38.193921+00:00 heroku[web.1]: State changed from down to starting
2019-05-28T08:52:36.858000+00:00 app[api]: Release v23 created by user example@gmail.com
2019-05-28T08:52:37.000000+00:00 app[api]: Build succeeded
2019-05-28T08:52:41.147852+00:00 heroku[web.1]: Starting process with command `npm start`
2019-05-28T08:52:43.738034+00:00 app[web.1]: 
2019-05-28T08:52:43.738051+00:00 app[web.1]: > webshop@1.0.0 start /app
2019-05-28T08:52:43.738053+00:00 app[web.1]: > node server.js
2019-05-28T08:52:43.738054+00:00 app[web.1]: 
2019-05-28T08:52:44.081058+00:00 app[web.1]: Server is running!
2019-05-28T08:52:45.135157+00:00 heroku[web.1]: State changed from starting to up

0 个答案:

没有答案