如何使用Node JS发布图像

时间:2018-12-02 02:52:09

标签: node.js image-uploading

嗨,我一直在尝试了解如何使用节点js发布图像,这是我的js文件代码。

我在这个js文件旁边有一个ejs文件, 但是当我通过终端运行此代码时,我在终端中收到了此消息。

/Users/thinking_nyum/Desktop/multiplemoon/app.js:9
  filename: function(req, file, cb){
  ^^^^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

这是否仅表示我的代码中存在拼写错误?还是我的节点文件夹中的文件已损坏?

const express = require('express');
const multer = require('multer');
const ejs = require('ejs');
const path = require('path');

// Set Storage engine
const storage = multer.diskStorage({
  destination: './public/uploads/'
  filename: function(req, file, cb){
    cb(null,file.fieldname + '-' + Date.now() + path.extname(file.originalname));

  }
});

//Init uploads
const upload = multer({
  storage: storage
}).single('myImage');


//Init app
const app = express();

//ejs
app.set('view engine', 'ejs');

//Public Folder
app.use(express.static('./public'));


app.get('/', (req, res) => res.render('index'));

app.post('/upload', (req, res) => {

      res.send('test');
    
});

const port = 3000;

app.listen(port, () => console.log(`Server started on port ${port}`));

我也会在此页面上附加我的ejs文件。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta hettp-equiv="X-UA-Compatible" content="ie=edge">
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

  <title>Node File Uploads</title>
</head>
<body>
  <div class="container">
    <h1>File Upload</h1>
    <%= typeof msg != 'undefined' ? msg : '' ; %>
  <form action="/upload" method"POST" enctype="multipart/form-data">
    <div class="file-field input-field">
      <div class="btn grey">
        <span>File</span>
        <input name="myImage" type="file">
      </div>
      <div class="file-path-wrapper">
        <input class="file-path validate" type="text">
      </div>
    </div>
    <button type="submit" class="btn black">Send</button>
  </form>
  </div>


  <script
    src="https://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</body>
</html>

0 个答案:

没有答案