【问题标题】:GET http://localhost:3000/server/server.js net::ERR_ABORTED 404 (Not Found)GET http://localhost:3000/server/server.js net::ERR_ABORTED 404(未找到)
【发布时间】:2019-12-11 13:47:51
【问题描述】:

我是 loopback 的新手。我正在使用 mysql 作为后端。我正在尝试将帐户列表显示到网页中,但是当我运行应用程序时出现问题,我在控制台窗口中遇到以下错误..

GET http://localhost:3000/server.js net::ERR_ABORTED 404(未找到) vue.js:634 [Vue 警告]:不要将 Vue 挂载到或 - 改为挂载到普通元素。

这是 server.js 的代码

// Copyright IBM Corp. 2016. All Rights Reserved.
// Node module: loopback-workspace
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

var loopback = require('loopback');
var boot = require('loopback-boot');

var app = module.exports = loopback();

app.start = function() {
  // start the web server
  return app.listen(function() {
    app.emit('started');
    var baseUrl = app.get('url').replace(/\/$/, '');
    console.log('Web server listening at: %s', baseUrl);
    if (app.get('loopback-component-explorer')) {
      var explorerPath = app.get('loopback-component-explorer').mountPath;
      console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
    }
  });
};

// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname, function(err) {
  if (err) throw err;

  // start the server if `$ node server.js`
  if (require.main === module)
    app.start();
});

这是我的html代码..

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">
</head>

<body id="AccountApp">
  <div>

    <h1>Account List</h1>
    <table>
      <thead>
        <tr>
          <th>Id</th>
          <th>Email Address</th>
          <th>Created Date</th>
          <th>Updated Date</th>
          <td>&nbsp;</td>
        </tr>
      </thead>
      <tbody>
        <tr v-for="account in accounts">
          <td @click="editAccount(account)" class="accountItem" title="Click to Edit">{{account.id}}</td>
          <td>{{account.email}}</td>
          <td>{{account.createdAt}}</td>
          <td>{{account.lastModifiedAt}}</td>

          <td @click="deleteAccount(account)" class="deleteAccount" title="Click to Delete">Delete</td>
        </tr>
      </tbody>
    </table>

    <form @submit.prevent="storeAccount">
      <p>
        <label for="email">Email</label>
        <input type="text" id="email" v-model="account.email">
      </p>
      <p>
        <label for="createdAt">Created At</label>
        <input type="text" id="createdAt" v-model="account.createdAt">
      </p>
      <p>
        <label for="lastModifiedAt">Last Modified At</label>
        <input type="text" id="lastModifiedAt" v-model="account.lastModifiedAt">
      </p>

      <input type="reset" value="Clear" @click="reset">
      <input type="submit" value="Save User ????">
    </form>
  </div>



</body>
</html>
<script src="server.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
<script>
  const API = 'http://localhost:3000/api/Account/';

  let AccountApp = new Vue({
    el: '#AccountApp',
    data: {
      accounts: [],
      account: {
        id: '',
        email: '',
        createdAt: '',
        lastModifiedAt: ''

      }
    },
    created: function () {
      this.getAccounts();
    },
    methods: {
      getAccounts: function () {
        fetch(API)
          .then(res => res.json())
          .then(res => this.account = res);
      },
      storeAccount: function () {
        let method;
        console.log('storeAccount', this.account);
        // Handle new vs old
        if (this.account.id === '') {
          delete this.account.id;
          method = 'POST';
        } else {
          method = 'PUT';
        }
        fetch(API, {
          headers: {
            'Content-Type': 'application/json'
          },
          method: method,
          body: JSON.stringify(this.account)
        })
          .then(res => res.json())
          .then(res => {
            this.getAccounts();
            this.reset();
          });
      },
      deleteAccount: function (c) {
        fetch(API + c.id, {
          headers: {
            'Content-Type': 'application/json'
          },
          method: 'DELETE'
        })
          .then(res => res.json())
          .then(res => {
            this.getAccounts();
          });

        // call reset cuz the cat could be 'active'
        this.reset();
      },
      editAccount: function (c) {
        /*
        This line was bad as it made a reference, and as you typed, it updated
        the list. A user may think they don't need to click save.
        this.cat = c;
        */
        this.account.id = c.id;
        this.account.email = c.email;
        this.account.createdAt = c.createdAt;
        this.account.lastModifiedAt = c.lastModifiedAt;

      },
      reset: function () {
        this.account.id = '';
        this.account.email = '';
        this.user.createdAt = '';
        this.user.lastModifiedAt = '';

      }
    }
  });
</script>

她是我运行应用程序时的屏幕截图。

【问题讨论】:

  • 你试过把id放在body里面的div上吗?
  • 感谢 phil 一个错误消失了,但为什么 GET localhost:3000/server.js net::ERR_ABORTED 404 (Not Found)
  • 你的脚本标签 src="server.js" 没有意义,你不应该需要这个。
  • 我在 mysql 数据库中有一些记录。我想在加载网页时显示该网页的这些记录,但是当我运行环回服务器时它是空的
  • server.js 看起来应该在服务器上运行,而不是在客户端上。

标签: mysql vue.js loopback


【解决方案1】:

您认为访问/server.js 有必要吗? 尝试为您的模型创建单独的控制器。 或者尝试根据您的需要创建模型。

供参考:https://loopback.io/doc/en/lb3/Defining-models.html

【讨论】:

    猜你喜欢
    • 2019-07-24
    • 1970-01-01
    • 2020-07-10
    • 2020-04-13
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2021-09-15
    • 2021-07-13
    相关资源
    最近更新 更多