简体   繁体   English

意外的标记 。 运行server.js时

[英]Unexpected Token . when running server.js

I'm currently writing a web application with the MEAN stack, and am testing to see if my nodejs server is working. 我目前正在使用MEAN堆栈编写一个Web应用程序,并且正在测试以查看我的nodejs服务器是否正常工作。 Here's my server.js: 这是我的server.js:

    // server.js
    'use strict';

    // modules =================================================
    const path = require('path');
    const express = require('express');
    const app = express();
    const bodyParser = require('body-parser');
    const methodOverride = require('method-override');

    // configuration ===========================================

    // config files
    const db = require('./config/db');

    // set our port
    var port = process.env.PORT || 8080;

    // connect to mongoDB
    // (uncomment after entering in credentials in config file)
    // mongoose.connect(db.url);

    // get all data/stuff of the body (POST) parameters
    // parse application/json
    app.use(bodyParser.json());

    // parse application/vnd.api+json as json
    app.use(bodyParser.json({ type: 'application/vnd.api+json' }));

    // parse application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: true }));

    // override with the X-HTTP-Method-Override header in the request simulate DELETE/PUT
    app.use(methodOverride('X-HTTP-Method-Override'));

    // set the static files location /public/img will be /img for users
    app.use(express.static(__dirname + '/public'));

    // routes ==================================================
    require('./app/routes')(app); // configure our routes

    // start app ===============================================
    // startup our app at http://localhost:8080
    app.listen(port);

    // shoutout to the user
    console.log('App running on port ' + port);

    // expose app
    exports = module.exports = app;

I currently have it redirecting all routes to my index.html file to test to make sure my views are working. 目前,我可以将所有路由重定向到我的index.html文件进行测试,以确保我的视图正常工作。 Here's my routes.js: 这是我的routes.js:

    // models/routes.js

    // grab the user model
    var User = require('./models/user.js');

    module.exports = {
        // TODO: Add all routes needed by application

        // frontend routes =========================================================
        // route to handle all angular requests
        app.get('*', function(req, res) {
            res.sendfile('./public/index.html'); // load our public/index.html  file
        });
    };

However, when I try to run node server.js , it gives me this error: 但是,当我尝试运行node server.js ,它给了我这个错误:

    /home/hess/Projects/FitTrak/app/routes.js
        app.get('*', function(req, res) {
           ^

    SyntaxError: Unexpected token .

Does anyone have any idea what's causing this? 有谁知道是什么原因造成的? I checked and all my braces and parentheses are all closed and written correctly. 我检查了一下,所有大括号和括号均已关闭并正确书写。

As Jose Hermosilla Rodrigo said in his comment, you're declaring the object literal module.exports wrong. 正如Jose Hermosilla Rodrigo在他的评论中所说,您声明的是对象常量module.exports错误。 It should look like this instead: 它应该看起来像这样:

module.exports = function(app) {
    app.get('*', function(req, res) {
        res.sendfile('./public/index.html'); // load our public/index.html  file
    });
};

just try this code... 只需尝试此代码...

// models/routes.js // models / routes.js

var express=require('express');
var app=express();

// TODO: Add all routes needed by application

        // frontend routes =========================================================
        // route to handle all angular requests
 app.get('*', function(req, res) {
            res.sendfile('./public/index.html');
        });

module.exports = route;

server.js server.js

'use strict';

const path = require('path');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
var route=require('./models/route.js');
const methodOverride = require('method-override');

// configuration ===========================================

// config files
const db = require('./config/db');

// set our port
var port = process.env.PORT || 8080;

// connect to mongoDB
// (uncomment after entering in credentials in config file)
// mongoose.connect(db.url);

// get all data/stuff of the body (POST) parameters
// parse application/json
app.use(bodyParser.json());

// parse application/vnd.api+json as json
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

// override with the X-HTTP-Method-Override header in the request simulate DELETE/PUT
app.use(methodOverride('X-HTTP-Method-Override'));

// set the static files location /public/img will be /img for users
app.use(express.static(__dirname + '/public'));

// routes ==================================================
require('./app/routes')(app); // configure our routes

// start app ===============================================
// startup our app at http://localhost:8080
app.listen(port);

// shoutout to the user
console.log('App running on port ' + port);


 app.use('/',route);

If you are using MEAN stack I would suggest you to use express own router middleware to handle all your routes. 如果您使用的是MEAN堆栈,建议您使用快速路由器中间件来处理所有路由。 Just include. 只是包括。

var router = express.Router();
    //use router to handle all your request
    router.get(/xxx,function(req, res){
    res.send(/xxxx);
    })

   // You may have n number of router for all your request
   //And  at last all you have to do is export router

     module.exports = router;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为server.js文件获取意外的令牌ILLEGAL错误 - Getting unexpected token ILLEGAL error for server.js file mern.io:server.js意外的令牌 - mern.io: server.js Unexpected token 运行server.js时NodeJS要求错误 - NodeJS require error when running the server.js 节点 server.js 需要不记名令牌 - Node server.js needs bearer token 为Azure IoT中心运行节点server.js时,``向RegExp构造函数提供了无效标志''错误 - 'Invalid flags supplied to RegExp constructor' error when running node server.js for Azure IoT Hub 运行我的 server.js 尝试连接到我的 sql db 时出现此语法错误 - I get this syntax error when running my server.js trying to connect to my sql db 运行节点server.js并重新播种db时,表将被删除。 Sequelize - Tables getting dropped when running node server.js and re-seeding db. Sequelize Razzle-在构建文件夹中运行server.js时未找到React错误 - Razzle - React not found Error on running server.js in build folder 在 Windows 的命令行中运行 node server.js 时,为什么会出现语法错误或服务器未定义错误? - Why am I getting a syntax error or server not defined error when running node server.js in command line for windows? 节点 server.js 没有响应 - node server.js not responding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM