簡體   English   中英

我可以在 Node.js 的不同端口上使用變量值嗎?

[英]Could I use variable values on different port in Node.js?

SyntaxError: 不能在模塊外使用 import 語句

索引.ts

let angle_ne = "Northeast";
export {angle_ne};
export let angle_nw = "Northwest";

索引.js

// next 2 line make "SyntaxError : Cannot use import statement outside a module".
// import angle_ne from "./index"
// console.log(angle_ne);

const express    = require('express');
const mysql      = require('mysql');
const dbconfig   = require('./database.js');
const connection = mysql.createConnection(dbconfig);
const app = express();

// configuration =========================
app.set('port', process.env.PORT || 3000);

app.get('/', (req, res) => {
  res.send('Root');
});

app.get('/address', (req, res) => {
  connection.query('SELECT * from address where Faddress like "%SEOUL%"', (error, rows) => {
    if (error) throw error;
    // console.log(angle_ne);
    console.log('Address info from DB : ', rows);
    res.send(rows);
    
  });
});

app.listen(app.get('port'), () => {
  console.log('Express server listening on port ' + app.get('port'));
});

在 Node.js 創建了一個與谷歌地圖的谷歌 Map API 相關的頁面。

index.ts:它由地圖和 API 相關的編碼部分組成,適用於 localhost:5173。 position被點擊的地址存儲在一個變量中,顯示在console.log中。

我在 localhost:3000 中建立了 MySQL 連接。 我嘗試在同一端口上運行 API 和 db,但它有一個問題,即 API 不起作用或 DB 不起作用。 所以我給了不同的端口。

我想在 index.js 中使用存儲在 index.ts 變量中的值。 目前的state有可能嗎? 如果可能,我應該找到什么? 還是因為端口不同而無法通過 export import 導入值?

我希望在控制台中顯示“東北”。 我嘗試從 index.ts 導出並導入到 index.js,它會生成“語法錯誤:無法在模塊外使用導入語句”。

我想使用這些值創建查詢。

您的語法似乎不一致。 您可以使用require()或 es6 導入/導出語法。 但不是兩者。

更多信息在這里: https://www.w3schools.com/nodejs/nodejs_modules.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM