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

标签 javascript node.js express mean-stack

我目前正在使用 MEAN 堆栈编写 Web 应用程序,并且正在测试我的 nodejs 服务器是否正常工作。这是我的 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;

我目前将所有路由重定向到我的 index.html 文件以进行测试以确保我的 View 正常工作。这是我的 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
        });
    };

但是,当我尝试运行 node server.js 时,它给了我这个错误:

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

    SyntaxError: Unexpected token .

有人知道是什么原因造成的吗?我检查了一下,我所有的大括号和圆括号都已关闭且书写正确。

最佳答案

作为Jose Hermosilla Rodrigo在他的评论中说,你正在声明对象文字 module.exports 错误。它应该看起来像这样:

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

关于javascript - 意外的标记 。运行 server.js 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37604411/

相关文章:

javascript - 将 OpenLayers 添加到 rails 项目

javascript - 使用 Javascript 删除 HTML 中的背景颜色和字体颜色

node.js - Hyperledger - 编写 REST API 或提交交易来添加/修改 Assets ?

reactjs - 使用 React Axios POST 请求时未设置 Express Session Cookie

node.js - 将 dotenv 路径与 JEST 一起使用

node.js - 在单个响应中返回 Mongoose 错误列表

php - 单击提交按钮时不使用重定向

javascript - 如何在集成测试中访问 ember 数据存储实例?

node.js - Restify Middleware - 正确调用堆栈中的下一个中间件

node.js - 同时 Mongo 插入是否有限制